JAVA接口资料翻译

原文:
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:

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. You’ll learn the reasons for this later in the lessons on Classes and Objects and Interfaces and Inheritance.
译文:
什么是接口?
正如你已经学到的,对象通过他们表现出来的方法义了他们与外界的相互作用。方法来自对象与外界的接口;比如电视机前的按钮,是一个例子,他是你和在塑料外壳另一边的电线之间的接口。你按下电源键来改变电视机的开关机状态。
在最常见的形式里,一个接口是一组有关联的空方法体。一辆自行车的行为,如果被指定为接口,就会像下面这样:
为了实现这个接口,你的类名会改变(举个例子,一辆特殊品牌的自行车,比如ACME牌自行车),并且你要在声明类时使用implements关键字。
实现一个接口能使一个类把他能够提供的行为变得更加正式。接口让类和外界形成了一个契约,而这个契约执行于编辑器创建它的时候。如果你的类要求实现一个接口,那么所有的方法必须在这个类成功编译之前,被这个接口定义在源码中。
注意:为了真正的编译ACME自行车类,你需要在实现接口的方法前加入public关键字。有将会在后面的类和对象以及接口和继承资料中学到这样做的原因。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值