你的位置:首页 > Java教程

[Java教程]009 The Interfaces In JAVA(官网文档翻译)

Interfaces

There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group's code is written. Generally speaking, interfaces are such contracts.

在软件工程的很多情形中,不想干的小组或程序员达成一致的合约(合约用于阐述软件如何交互)是非常重要的。在合约的帮助下,每个小组写代码时不必了解其它小组的代码时如何编写的。一般来说,这样的合约由接口来充当。

For example, imagine a futuristic society where computer-controlled robotic cars transport passengers through city streets without a human operator. Automobile manufacturers write software (Java, of course) that operates the automobile—stop, start, accelerate, turn left, and so forth. Another industrial group, electronic guidance instrument manufacturers, make computer systems that receive GPS (Global Positioning System) position data and wireless transmission of traffic conditions and use that information to drive the car.

譬如说,想象一下在不久的将来,由电脑控制的机器人汽车载着乘客穿过城市街道,而车是无人驾驶的。而汽车制造商用JAVA编写的软件操作汽车——停止、启动、加速、向左转等等。而另一个产业群电子制导仪器制造商使用电脑系统接收GPS数据和交通状况的无线传输数据,并用接收到的数据驾车汽车。

The auto manufacturers must publish an industry-standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer). The guidance manufacturers can then write software that invokes the methods described in the interface to command the car. Neither industrial group needs to know how the other group's software is implemented. In fact, each group considers its software highly proprietary and reserves the right to modify it at any time, as long as it continues to adhere to the published interface.

该汽车的制造商必须发布行业标准接口,阐述调用什么方法可以使车移动,然后指导制造商可以调用接口描述中的方法控制汽车,而工业集团不必知道其他集团的软件是如何实现的。事实上,每个集团都认为自己对软件高度专有,可以随时修改软件,前提是遵循发布的接口规范。