为什么我们需要 Java 中的接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3528420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do we need interfaces in Java?
提问by Vinoth Kumar
In Java to implement multiple inheritance we use interfaces. Is it the only use of interfaces? If yes, what is the main use of interface in Java? Why do we need interfaces in Java?
在 Java 中,我们使用接口来实现多重继承。它是唯一使用的接口吗?如果是,Java 中接口的主要用途是什么?为什么我们需要 Java 中的接口?
回答by Vinoth Kumar
I would say the main use is polymorphism, or the ability to perform the same operation on a number of different objects. If different objects all implement the same interface and have the same method, you can store all of those objects in a Vector, for example, and iterate through the Vector calling that method on each one.
我会说主要用途是多态性,或对许多不同对象执行相同操作的能力。例如,如果不同的对象都实现相同的接口并具有相同的方法,则可以将所有这些对象存储在一个 Vector 中,例如,并遍历 Vector 对每个对象调用该方法。
回答by Amadan
You need them so you can type your objects outside the hierarchy.
您需要它们,以便您可以在层次结构之外键入您的对象。
For example, the objects that can be compared can be pretty much anywhere on the object hierarchy - they do not need to have a common ancestor which can be compared. String
s can be compared, Integer
s can be compared, you could even make your own Frame
s that could be compared (say, a frame is "less" than another frame if it is more in the foreground - i.e. if it would overlay the other frame). Thus, if you want to refer to a thing that can be compared, you would be forced to declare a variable with the most general ancestor - in this case, Object
. This is too general, because then it can also receive values which are not comparable (and would throw errors when you try to compare them).
例如,可以比较的对象几乎可以位于对象层次结构中的任何位置——它们不需要具有可以比较的共同祖先。String
s 可以比较,Integer
s 可以比较,您甚至可以制作自己的Frame
s 进行比较(例如,如果一个帧在前景中更多,则它比另一个帧“少” - 即如果它会覆盖另一个帧) . 因此,如果你想引用一个可以比较的东西,你将被迫声明一个具有最一般祖先的变量 - 在这种情况下,Object
. 这太笼统了,因为它也可以接收不可比较的值(并且在您尝试比较它们时会抛出错误)。
Thus, the interface Comparable
: it selects all the classes that implement the comparison functionality acrossthe subclass-superclass hierarchy.
因此,接口Comparable
:它选择实现跨子类-超类层次结构的比较功能的所有类。
回答by Peter Tillemans
In addition to these responses I would say the most important use for interfaces is to reduce coupling between components in your software.
除了这些响应之外,我认为接口最重要的用途是减少软件中组件之间的耦合。
An interface allows to represent an agreement between classes on how they will talk to each other without being tied to the actual implementations.
接口允许表示类之间关于它们将如何相互交谈而不与实际实现相关联的协议。
This allows us to replace implementations by others (very useful for testing, or changing use cases) without changing the compiled code.
这允许我们在不更改编译代码的情况下替换其他人的实现(对于测试或更改用例非常有用)。
回答by Danny Crossley
Some code won't compile without it.
没有它,有些代码将无法编译。
For example, in:
例如,在:
for (String name : list)
{
System.out.print("\nIn foreach loop: name: " + name);
}
list
must implement the java.lang.Iterable interface
.
list
必须实现java.lang.Iterable interface
.
回答by Thein
I was also thinking about how interfaces are used. I hope this will help others:
我也在考虑如何使用接口。我希望这会帮助其他人:
An interface is a contract (or a protocol, or a common understanding) of what the classes can do. When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface. Interface defines a set of common behaviors. The classes implement the interface agree to these behaviors and provide their own implementation to the behaviors. This allows you to program at the interface, instead of the actual implementation. One of the main usage of interface is provide a communication contract between two objects. If you know a class implements an interface, then you know that class contains concrete implementations of the methods declared in that interface, and you are guaranteed to be able to invoke these methods safely. In other words, two objects can communicate based on the contract defined in the interface, instead of their specific implementation.
Secondly, Java does not support multiple inheritance (whereas C++ does). Multiple inheritance permits you to derive a subclass from more than one direct superclass. This poses a problem if two direct superclasses have conflicting implementations. (Which one to follow in the subclass?). However, multiple inheritance does have its place. Java does this by permitting you to "implements" more than one interfaces (but you can only "extends" from a single superclass). Since interfaces contain only abstract methods without actual implementation, no conflict can arise among the multiple interfaces. (Interface can hold constants but is not recommended. If a subclass implements two interfaces with conflicting constants, the compiler will flag out a compilation error.)
接口是类可以做什么的契约(或协议,或共同理解)。当一个类实现某个接口时,它承诺为接口中声明的所有抽象方法提供实现。接口定义了一组常见的行为。实现接口的类同意这些行为,并为这些行为提供自己的实现。这允许您在接口上编程,而不是实际实现。接口的主要用途之一是在两个对象之间提供通信契约。如果您知道一个类实现了一个接口,那么您就知道该类包含在该接口中声明的方法的具体实现,并且您可以保证能够安全地调用这些方法。换句话说,
其次,Java 不支持多重继承(而 C++ 支持)。多重继承允许您从多个直接超类派生一个子类。如果两个直接超类具有冲突的实现,这就会带来问题。(在子类中遵循哪一个?)。然而,多重继承确实有其一席之地。Java 通过允许您“实现”多个接口来实现这一点(但您只能从单个超类“扩展”)。由于接口只包含抽象方法而没有实际实现,因此多个接口之间不会发生冲突。(接口可以保存常量,但不推荐。如果一个子类实现了两个具有冲突常量的接口,编译器将标记出一个编译错误。)
from: http://www.ntu.edu.sg/home/ehchua/programming/java/J3b_OOPInheritancePolymorphism.html#zz-6.6
来自:http: //www.ntu.edu.sg/home/ehchua/programming/java/J3b_OOPInheritancePolymorphism.html#zz-6.6