Documentation

The Java™ Tutorials
Hide TOC
What Is an Interface?什么是接口?
Trail: Learning the Java Language
Lesson: Object-Oriented Programming Concepts

What Is an Interface?什么是接口?

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.如果类声明实现接口,则该接口定义的所有方法必须出现在其源代码中,然后该类才能成功编译。


Note: To actually compile the ACMEBicycle class, you'll need to add the public keyword to the beginning of the implemented interface methods.要实际编译ACMEBicycle类,需要将public关键字添加到实现的接口方法的开头。You'll learn the reasons for this later in the lessons on Classes and Objects and Interfaces and Inheritance.在后面关于类和对象接口和继承的课程中,您将了解其原因。

Previous page: What Is Inheritance?
Next page: What Is a Package?