There's the English version of this article. 這邊有英文版的文章。
原本要學習 Design Patterns (設計模式),結果驚覺 JAVA 的 interface超重要,
因為JAVA實在太久沒用了...為了 Design Patterns 先來複習一下interface!
介面(interface)簡單來說,是設計用途!
可先設計一定要被實作(implements)的方法(名字跟回傳型態),而不用先定義程式內容。
聽起來是不是跟abstract的方法很像? 雖然沒有abstract,但他就是!
(可以同時包裝很多抽象的東東!)
多個class可以同時實作相同interface;
一個類別可以同時實作很多interface。
(因為JAVA不提供多重繼承,所以可以用這樣達到類似效果。)
二話不說來看下面範例,雖然很簡單,可能無法表現 interface的精隨。
但是至少可以執行嘛...學習要由簡往繁!
延伸閱讀:
If you want to use (copy, paste or quote) my original article, please contact me through email (autek.roy@gmail.com). If there is any mistake or comment, please let me know. :D
如要使用(複製貼上或轉載)作者原創文章,請來信跟我聯絡 (autek.roy@gmail.com)。如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
原本要學習 Design Patterns (設計模式),結果驚覺 JAVA 的 interface超重要,
因為JAVA實在太久沒用了...為了 Design Patterns 先來複習一下interface!
介面(interface)簡單來說,是設計用途!
可先設計一定要被實作(implements)的方法(名字跟回傳型態),而不用先定義程式內容。

(可以同時包裝很多抽象的東東!)
多個class可以同時實作相同interface;
一個類別可以同時實作很多interface。
(因為JAVA不提供多重繼承,所以可以用這樣達到類似效果。)
二話不說來看下面範例,雖然很簡單,可能無法表現 interface的精隨。
但是至少可以執行嘛...學習要由簡往繁!
這是interface的設計,設計一個簡單叫聲行為,宣告了sound()函數。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @interface SoundBehavior | |
*/ | |
public interface SoundBehavior { | |
String sound(); | |
} |
建立動物的class,有建構式跟 toString()。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Animal { | |
private String type;//the type of animal | |
public Animal(String inputType){ | |
this.type = inputType; | |
} | |
public String toString(){ | |
return type; | |
} | |
} |
Cat 貓咪的class,繼承動物而來,並實作介面(SoundBehavior)中的sound()。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Cat extends Animal implements SoundBehavior{ | |
private String name; | |
public Cat(String inputName){ | |
super("Cat");//call the Animal class constructor, type is "Cat" | |
this.name = inputName; | |
} | |
public String sound(){ | |
return "喵喵 Miaow"; | |
} | |
public String toString(){ | |
return name; | |
} | |
} |
執行的主程式~
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class interfaceTest { | |
/** | |
* @param interfaceTest | |
* @purpose test the interface | |
* Date: 05/14/2014 22:00 in Taiwan | |
* author: wing (http://autekroy.blogspot.com/) | |
*/ | |
public static void main(String[] args) { | |
Cat myCat = new Cat("提摩"); | |
Cat yourCat = new Cat("波比"); | |
System.out.println("我的貓名字是" + myCat); | |
System.out.println(myCat + "的叫聲是" + myCat.sound()); | |
System.out.println(); | |
System.out.println("你的貓名字是" + yourCat); | |
System.out.println(yourCat + "的叫聲是" + yourCat.sound()); | |
} | |
} |
執行結果:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
我的貓名字是提摩 | |
提摩的叫聲是喵喵 Miaow | |
你的貓名字是波比 | |
波比的叫聲是喵喵 Miaow |
延伸閱讀:
If you want to use (copy, paste or quote) my original article, please contact me through email (autek.roy@gmail.com). If there is any mistake or comment, please let me know. :D
如要使用(複製貼上或轉載)作者原創文章,請來信跟我聯絡 (autek.roy@gmail.com)。如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
en → zh-TW
歡迎加入這個博客作為參考後使用它。 ( http://autekroy.blogspot.tw )如果有任何錯誤或意見,請讓我知道。 :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/)如果有發現任何的錯誤與建議請留言或跟我連絡。 )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!