interface這個關鍵字產生一個完全抽象的類,它根本就沒有提供任何具體的實現,它允許創建者確定方法名.參數列表和返回類型,但沒有任何方法體,接口只提供了形式,而未提供任何具體實現
一個接口表示:"所有實現了該特定接口的類看起來都像這樣".接口被用來建立類與類之間的協議(某些面向對象語言用關鍵字protocol來完成這一功能.)
要想創建一個接口,需要用interface關鍵字代替class關鍵字,就像類一樣,可以在interface前添加public關鍵字(但僅限於該接口在與其同名的文件中被定義),如果不加public,則只有包訪問權限,這樣就只能在同一個包內可用,接口也可以包含域,但是這些域都隱式地是static和final的
要讓一個類遵循某個特定接口(或者是一組接口),需要使用implements關鍵字,它表示"interface只是它的外貌,但是現在我要聲明它如何工作的."除此之外,它看起來很像繼承
恰當的原則是優先選擇類而不是接口,從類開始,如果接口的必要性變的非常明確,那么就進行重構.
//: interfaces/music5/Music5.java // Interfaces. package object; import static net.mindview.util.Print.*; enum Note{ MIDDLE_C,MIDDLE_D,MIDDLE_F } interface Instrument { // Compile-time constant: int VALUE = 5; // static & final 可以聲明域,但這些域都隱式地是static和final的 // Cannot have method definitions: void play(Note n); // Automatically public //自動的是public void adjust(); } class Wind implements Instrument { public void play(Note n) { print(this + ".play() " + n); } public String toString() { return "Wind"; } public void adjust() { print(this + ".adjust()"); } } class Percussion implements Instrument { public void play(Note n) { print(this + ".play() " + n); } public String toString() { return "Percussion"; } public void adjust() { print(this + ".adjust()"); } } class Stringed implements Instrument { public void play(Note n) { print(this + ".play() " + n); } public String toString() { return "Stringed"; } public void adjust() { print(this + ".adjust()"); } } class Brass extends Wind { public String toString() { return "Brass"; } } class Woodwind extends Wind { public String toString() { return "Woodwind"; } } public class Music5 { // Doesn't care about type, so new types // added to the system still work right: static void tune(Instrument i) { // ... i.play(Note.MIDDLE_C); } static void tuneAll(Instrument[] e) { for(Instrument i : e) tune(i); } public static void main(String[] args) { // Upcasting during addition to the array: Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind() }; tuneAll(orchestra); } } /* Output: Wind.play() MIDDLE_C Percussion.play() MIDDLE_C Stringed.play() MIDDLE_C Brass.play() MIDDLE_C Woodwind.play() MIDDLE_C *///:~
繼承和接口可以在同一個類同時使用
//: polymorphism/Sandwich.java // Order of constructor calls. package ch08; interface FastFood{ void show(); } class Meal { Meal() { System.out.println("Meal()"); } } class Bread { Bread() { System.out.println("Bread()"); } } class Cheese { Cheese() { System.out.println("Cheese()"); } } class Lettuce { Lettuce() { System.out.println("Lettuce()"); } } class Lunch extends Meal { Lunch() { System.out.println("Lunch()"); } } class PortableLunch extends Lunch { PortableLunch() { System.out.println("PortableLunch()");} } public class Sandwich extends PortableLunch implements FastFood{ //繼承和接口可以在同一個類同時使用 private Bread b = new Bread(); private Cheese c = new Cheese(); private Lettuce l = new Lettuce(); public void show() { System.out.println("pushing your sandwich order"); } public Sandwich() { System.out.println("Sandwich()"); show(); } public static void main(String[] args) { new Sandwich(); } } /* Output: Meal() Lunch() PortableLunch() Bread() Cheese() Lettuce() Sandwich()
pushing your sandwich order
*///:~