interface这个关键字产生一个完全抽象的类,它根本就没有提供任何具体的实现,它允许创建者确定方法名.参数列表和返回类型,但没有任何方法体,接口只提供了形式,而未提供任何具体实现
一个接口表示:"所有实现了该特定接口的类看起来都像这样".接口被用来建立类与类之间的协议(某些面向对象语言用关键字protocol来完成这一功能.)
要想创建一个接口,需要用interface关键字代替class关键字,就像类一样,可以在interface前添加public关键字(但仅限于该接口在与其同名的文件中被定义),如果不加public,则只有包访问权限,这样就只能在同一个包内可用,接口也可以包含域,但是这些域都隐式地是static和final的
要让一个类遵循某个特定接口(或者是一组接口),需要使用implements关键字,它表示"interface只是它的外貌,但是现在我要声明它如何工作的."除此之外,它看起来很像继承
恰当的原则是优先选择类而不是接口,从类开始,如果接口的必要性变的非常明确,那么就进行重构.
//: interfaces/music5/Music5.java//Interfaces.
packageobject;import static net.mindview.util.Print.*;enumNote{
MIDDLE_C,MIDDLE_D,MIDDLE_F
}interfaceInstrument {//Compile-time constant:
int VALUE = 5; //static & final 可以声明域,但这些域都隐式地是static和final的//Cannot have method definitions:
void play(Note n); //Automatically public//自动的是public
voidadjust();
}class Wind implementsInstrument {public voidplay(Note n) {
print(this + ".play() " +n);
}public String toString() { return "Wind"; }public void adjust() { print(this + ".adjust()"); }
}class Percussion implementsInstrument {public voidplay(Note n) {
print(this + ".play() " +n);
}public String toString() { return "Percussion"; }public void adjust() { print(this + ".adjust()"); }
}class Stringed implementsInstrument {public voidplay(Note n) {
print(this + ".play() " +n);
}public String toString() { return "Stringed"; }public void adjust() { print(this + ".adjust()"); }
}class Brass extendsWind {public String toString() { return "Brass"; }
}class Woodwind extendsWind {public String toString() { return "Woodwind"; }
}public classMusic5 {//Doesn't care about type, so new types//added to the system still work right:
static voidtune(Instrument i) {//...
i.play(Note.MIDDLE_C);
}static voidtuneAll(Instrument[] e) {for(Instrument i : e)
tune(i);
}public static voidmain(String[] args) {//Upcasting during addition to the array:
Instrument[] orchestra ={newWind(),newPercussion(),newStringed(),newBrass(),newWoodwind()
};
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.
packagech08;interfaceFastFood{voidshow();
}classMeal {
Meal() { System.out.println("Meal()"); }
}classBread {
Bread() { System.out.println("Bread()"); }
}classCheese {
Cheese() { System.out.println("Cheese()"); }
}classLettuce {
Lettuce() { System.out.println("Lettuce()"); }
}class Lunch extendsMeal {
Lunch() { System.out.println("Lunch()"); }
}class PortableLunch extendsLunch {
PortableLunch() { System.out.println("PortableLunch()");}
}public class Sandwich extends PortableLunch implementsFastFood{ //继承和接口可以在同一个类同时使用private Bread b = newBread();private Cheese c = newCheese();private Lettuce l = newLettuce();public voidshow()
{
System.out.println("pushing your sandwich order");
}publicSandwich()
{
System.out.println("Sandwich()");
show();
}public static voidmain(String[] args) {newSandwich();
}
}/*Output:
Meal()
Lunch()
PortableLunch()
Bread()
Cheese()
Lettuce()
Sandwich()pushing your sandwich order
*///:~