gpt4 book ai didi

java - Java中接口(interface)的有用范围

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:26:54 26 4
gpt4 key购买 nike

接口(interface)由抽象方法和最终变量组成。好吧,它被用作提出的通用契约,因此实现它的类应该通过在其中实现方法来遵循规则。

这是 Java 接口(interface)的唯一用途/范围吗?他们是否为此引入了接口(interface)的概念,还是我遗漏了什么?请通过示例帮助我理解接口(interface)的使用。 (不是关于如何使用或创建接口(interface),而是展示它们如何帮助程序员)。

谢谢。

最佳答案

这是我第一次阅读它们时了解它们用法的地方:

Say that you receive a portable disc player as a gift. When you try to operate the player, nothing happens --- the player requires batteries. What batteries fit into the player? Fortunately, on the back of the player is the specification, This player requires two AA
batteries.'' With this information, you can obtain the correctly sized
components (the batteries) and fit them into the player. The completed
assembly'' operates.

The specification of the disc player's batteries served several useful purposes:

The specification told the user which component must be fitted to the player to ensure correct operation. The specification told the manufacturer of the disc player what size to build the player's battery chamber and what voltage and amperage to use within the player's electronics. The specification told the battery manufacturer what size, voltage, and amperage to build batteries so that others can use them. These three facts are important in themselves, but they also imply that the user, the disc manufacturer, and the battery manufacturer need not communicate directly with each other --- the specification of the battery is all that is needed for each party to perform its own task independently of the other two.

关于java - Java中接口(interface)的有用范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8999410/

26 4 0