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
*///:~