張世旭
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 類別實作介面 ```java= public interface IMoter { public void moter(); //告訴幾cc.. } //要使用介面, 創立類別實作介面.. public class Toyata implements IMoter{ //會出現紅字, 因還位實作moter() } ===往下修改.. public class Toyata implements IMoter{ //按alt+enter,選實作抽像方法.. @Override public void moter() { System.out.println("2000cc"); } } =====主測式程式. public class A1122 { public static void main(String[] args) { System.out.println("Hello World!"); Toyata car1=new Toyata(); car1.moter(); } } ``` # 使用匿名類別 >使用介面 要透過類別實作, 例如Toyata.. >若不想透過類別實作 給類別名稱..可使用匿名類別.. >使用匿名類別. ```java= IMoter car2=new IMoter(){ //alt+enter }; 加入以下方法: IMoter car2=new IMoter(){//alt+enter @Override public void moter() { System.out.println("2000cc in 匿名類別"); } }; car2.moter(); ===主測式程式 public class A1122 { public static void main(String[] args) { System.out.println("Hello World!"); Toyata car1=new Toyata(); car1.moter(); IMoter obj=new IMoter(){ //alt+enter @Override public void moter() { System.out.println("hello moter is 2000cc"); } }; obj.moter(); new IMoter(){ @Override public void moter() { System.out.println("2000 cc"); } }.moter(); } } ``` # 介面的方法加上default宣告 > 一般界面內的方法宣告 不能有實作程式碼 { } > java 8.0.. 只要加入default宣告後..就可實作程式碼.. ```java= public interface IMoter { public void moter(); //告訴幾cc.. //一般界面內的方法宣告 不能有實作程式碼 {} //java 8.0.. 只要加入default宣告後..就可實作程式碼.. public default void do1(){ //因加了default宣告..ok. moter(); //default方法一般是呼叫 abstract方法..例如moter() } public default void do2(){ moter(); System.out.println("do2() in IMoter"); } } ===主測式程式 public class A1122 { public static void main(String[] args) { new IMoter(){ @Override public void moter() { System.out.println("2000 cc in new IMoter(){} "); } }.do1(); //do1()是defulat宣告已有程式碼 new IMoter(){ @Override public void moter() { System.out.println("3000 cc in new IMoter(){} "); } @Override public void do1(){ //可蓋掉介面中defulat方法 System.out.println("do1()..."); moter(); } }.do1(); } } ``` # 類別沒寫extends = 繼承Object > 類別沒有寫extends, 一般就是直接繼承Object ```java= public class Animal { // extends Object{ //沒有寫一般就是直接繼承Object //所以就省略 extends Object String name; int age; public Animal(String name,int age){ this.name=name; this.age=age; } public void working(){ System.out.println(name + " " + age + " years old is working()"); } @Override //toString 是Object類別裡的方法..繼承者修改它 要加@Override public String toString(){ //回傳一個你設定的字串..當列印此物件時呼叫toString() return name + " " + age + " years old is working()"; } } # 類別People繼承Animal 類別People繼承Animal{ //為何要繼承..因想新增一些方法..例如 新增吃的方法 eat() //或者改寫 Override 原先的方法. } public class People extends Animal{ } 新增吃的方法.. public class People extends Animal{ public void eat(){ System.out.println("用手持餐具盛食物就食"); } //按右鍵 選Insert Code..選Override code.. @Override public void working() { super.working(); System.out.println("用兩隻腳走路"); } } public class People extends Animal{ //因為父類別 Animal建構子只有兩個參數的..因此也要有兩個參數的建構子.. public People(){ super(null, 0); //呼叫父類別的建構子.等於呼叫Animal(null,0) } public People(String name, int age){ super(name, age);//呼叫父類別的建構子, 等於呼叫Animal(name,age) } public void eat(){ System.out.println("用手持餐具盛食物就食"); } //按右鍵 選Insert Code..選Override code.. @Override public void working() { //super.working(); System.out.println("用兩隻腳走路"); } } ==主測試程式 public class A1122 { public static void main(String[] args) { People person1=new People("wang", 12); person1.working(); } } ``` # 匿名 類別 > 子類別名稱想要匿名.. > 直接使用 new 父類別() { 程式碼; }; ```java= public class A1122 { public static void main(String[] args) { People person1=new People("wang", 12); person1.working(); //匿名 介面 interface.. //匿名 類別.. 可使用var 宣告型態.. var obj1=new Animal("chen", 20){ public void eat(){ System.out.println("用手持餐具盛食物就食"); } @Override public void working() { super.working(); System.out.println("用兩隻腳走路"); } }; //可使用var 宣告型態.. obj1.working(); //可直接new 物件後直接呼叫使用. new Animal("chen", 20){ public void eat(){ System.out.println("用手持餐具盛食物就食"); } @Override public void working() { super.working(); System.out.println("用兩隻腳走路"); } }.working(); } } ``` > 若是想要匿名 創新的類別..可使用下法 ```java= //若是想要匿名 創新的類別..可使用下法, 但只能無參數的建構子.. new Object(){ public void working(){ System.out.println("working()...."); } }.working(); ``` # [介面語法細節](https://openhome.cc/zh-tw/java/interface/syntax/) # 介面Comparator <T> java.util.Comparators https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html java.util Interface Comparator<T> https://www.javatpoint.com/Comparator-interface-in-collection-framework https://www.geeksforgeeks.org/comparator-interface-java/ ```java= public class Student { String name; int age; int score; public Student(String name, int age, int score){ this.name=name; //用this代表此物件. 區分變數 this.age=age; this.score=score; } //按右鍵 insert code ..選Override method.. @Override public String toString() { String str=name+" "+age + " " + score; return str; } } ====== 創立類別StudentSort 實作界面Comparator import java.util.Comparator; public class StudentSort implements Comparator{//alt+enter //選add import java.util.Comparator //再一次alt+enter, 選implements abstract method @Override public int compare(Object o1, Object o2) { return 0; } } ====================== 改成針對Student的比較.. Comparator 後面加上 <Student> public class StudentSort implements Comparator <Student>{//alt+enter //選add import java.util.Comparator //再一次alt+enter, 選implements abstract method @Override public int compare(Student o1, Student o2) { return 0; } } ====================== 修改compare方法.. public class StudentSort implements Comparator <Student>{//alt+enter //選add import java.util.Comparator //再一次alt+enter, 選implements abstract method @Override public int compare(Student o1, Student o2) { //使用age來排序.. if(o1.age>o2.age) return 1; else if(o1.age < o2.age) return -1; else return 0; } } public class A1122 { public static void main(String[] args) { Student stu1=new Student("wang", 18, 60); Student stu2=new Student("chen", 23, 100); Student stu3=new Student("liu", 15, 40); Student [] arr={stu1, stu2, stu3}; for(Student s:arr){ System.out.println(s); } //Arrays.sort(類別, 有實作Comparator的物件 ); //static <T> void sort(T[] a, Comparator<? super T> c) Arrays.sort(arr, new StudentSort()); for(Student s:arr){ System.out.println(s); } } } 輸出結果: wang 18 60 chen 23 100 liu 15 40 排序後: (按age由小排到大) liu 15 40 wang 18 60 chen 23 100 public class A1122 { public static void main(String[] args) { Student stu1=new Student("wang", 18, 60); Student stu2=new Student("chen", 23, 100); Student stu3=new Student("liu", 15, 60); Student stu4=new Student("tan", 15, 60); Student stu5=new Student("lu", 15, 60); Student [] arr={stu1, stu2, stu3,stu4, stu5}; System.out.println("排序前:"); for(Student s:arr){ System.out.println(s); } //Arrays.sort(類別, 有實作Comparator的物件 ); //static <T> void sort(T[] a, Comparator<? super T> c) Arrays.sort(arr, new StudentSort()); System.out.println("排序後:"); for(Student s:arr){ System.out.println(s); } } } ================================= public int compare(Student stu1, Student stu2){ if(stu1.score>stu2.score) return 1; //1,-1是切換排序順序..由小到大..由大到小.. else if(stu1.score<stu2.score) return -1; //-1, 1 是切換排序順序 配合上面的修改.. else{ //0 score相同..比較age.. if(stu1.age>stu.age) return 1; else if(stu1.age<stu.age) return -1; else {//這裡是score相同,age相同..再比較name. //調整1,-1的值. int result=stu1.name.compareTo(stu2.name); if(result>0) return 1; else if(result < 0) return -1; else return 0; } } } ====不一定要回傳1, -1, 也可正負數, 可改成直接相減, public int compare(Student stu1, Student stu2){ int result=stu1.score-stu2.score; //或result=stu2.score-stu1.score if(result !=0) return result; else{ result=stu1.age-stu2.age; //或result= stu2.age-stu1.age if(result !=0) return result; else{ result=stu1.name.compareTo(stu2.name); //或 result=stu2.name.compareTo(stu1.name); return result; } } } ``` <hr> String 使用 compareTo() 方法比較 Java 中的字串 它對兩個字串進行詞法上的比較。它的工作原理是首先給字串中的每個字元分配一個 Unicode 值,然後比較這兩個字串。這個過程將結果返回為零、正數或負數。 compareTo() 區分大小寫的 當兩個比較的字串在詞法上相等時,結果為零。 如果第一個字串比第二個字串大,結果為正數。 當第二個字串大於第一個字串時,結果為負。 <hr> ```java= String string1 = "hello"; String string2 = "world"; int result = string1.compareTo(string2); if(result >0) return 1; else if(result <0) return -1; else return 0; //compareToIgnoreCase() 忽略了大小寫 int result = string1.compareToIgnoreCase(string2); ``` <hr> ===== # 11/22 作業: 按score由大排到小...修改compare()中的內容.. 若同分的..再按age由小排到大 若age再相同..則按名字a--z 排序 <hr> https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html static <T> void sort(T[] a, Comparator<? super T> c) https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-T:A-java.util.Comparator-

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully