Java 介面關鍵字 interface
阿新 • • 發佈:2019-01-01
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: intVALUE = 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
*///:~