The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
As you've already learned, objects define their interaction with the outside world through the methods that they expose.正如您已经了解到的,对象通过它们公开的方法定义它们与外部世界的交互。Methods form the object's interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing.方法形成对象与外部世界的接口;例如,电视机正面的按钮是您与塑料外壳另一侧电线之间的接口。You press the "power" button to turn the television on and off.你按下“电源”按钮打开和关闭电视。
In its most common form, an interface is a group of related methods with empty bodies.在其最常见的形式中,接口是一组具有空实体的相关方法。A bicycle's behavior, if specified as an interface, might appear as follows:如果将自行车的行为指定为接口,则其行为可能如下所示:
interface Bicycle { // wheel revolutions per minute void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); }
To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as 要实现此接口,类的名称将更改(例如,更改为特定品牌的自行车,如ACMEBicycle
), and you'd use the implements
keyword in the class declaration:ACMEBicycle
),并且您将在类声明中使用implements
关键字:
class ACMEBicycle implements Bicycle { int cadence = 0; int speed = 0; int gear = 1; // The compiler will now require that methods // changeCadence, changeGear, speedUp, and applyBrakes // all be implemented. Compilation will fail if those // methods are missing from this class. void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); } }
Implementing an interface allows a class to become more formal about the behavior it promises to provide.实现一个接口可以使一个类在其承诺提供的行为方面变得更加正式。Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.接口在类和外部世界之间形成契约,编译器在构建时强制执行该契约。If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.如果类声明实现接口,则该接口定义的所有方法必须出现在其源代码中,然后该类才能成功编译。
ACMEBicycle
class, you'll need to add the public
keyword to the beginning of the implemented interface methods.ACMEBicycle
类,需要将public
关键字添加到实现的接口方法的开头。