java中接口的实际用途是什么?
可能的重复:
抽象类和接口类?
Java:接口/抽象类/抽象方法
在Java中,无论使用什么接口由抽象类来实现。我知道接口的一个优点是,如果我们实现一个接口,那么我们还可以扩展另一个类。 Java中的接口还有其他用途或优点吗?
Possible Duplicates:
Abstract class and Interface class?
Java: interface / abstract classes / abstract method
In Java, whatever use of interface is fulfilled by abstract class. I know one advantage of interfaces is that if we implement an interface then we can also extend another class. Is there any other use or advantage of interface in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
接口允许您多态使用不同层次结构中的类。
例如,假设您有以下接口:
跨类层次结构的任何数量的类都可以以自己的特定方式实现
Movable
,但仍然可以被某些调用者使用统一的方式。因此,如果您有以下两个类:
从调用者的角度来看,它只是一个
Movable
我希望这会有所帮助。
Interfaces allow you to use classes in different hierarchies, polymorphically.
For example, say you have the following interface:
Any number of classes, across class hierarchies could implement
Movable
in their own specific way, yet still be used by some caller in a uniform way.So if you have the following two classes:
From the perspective of the caller, it's just a
Movable
I hope this helps.
你喜欢什么:在一个抽象类中包含数千个抽象方法并继承这个类,或者为特定的抽象方法创建尽可能多的接口,并通过继承尽可能多的接口来仅使用你想要的那些接口......
所以,只使用你只需要的方法通过继承特定的接口,如果您继承
Abstract
类,那么您就不必要地继承了一个类中不需要但某些其他类中可能需要的所有方法。What you like : thousands of abstract methods in one Abstract Class and inherit this class OR make as many interfaces for specific abstract methods and use those only you want by inheriting as many interfaces as needed...
So, use that method only what you just need by inheriting particular interface, if you are inheriting
Abstract
classes then you are unnecessarily inheriting all methods that you don't need in one class and may be needed in some other classes..可以实现多个接口,但只能扩展一个类。完全抽象的类很像接口,只不过抽象类可以包含变量。
这实际上取决于您的需要。 C++ 允许您扩展任意数量的类,但这会变成一场灾难。只有一个超类的好处是,您只需要担心另一组实现(即使父类有一个父类,父类的特定组合也将成为您的父类......)
接口允许一个对象发挥作用许多角色,但它们不允许代码重用。
这实际上是为了简化对继承的思考。总的来说,我认为他们做对了。
Multiple interfaces can be implemented, but only one class can be extended. A completely abstract class is a lot like an interface, except that an abstract class can contain variables.
It really depends on what you need. C++ allows you to extend as many classes you want, and it turns into a bit of a disaster. The nice thing about having only one superclass is that there's only ever one other set of implementations that you have to worry about (even if the parent has a parent, the parent's particular combination becomes your parent...)
Interfaces allow one object to play many roles, but they don't allow code reuse.
It's really to simplify thinking about inheritance. On the balance, I think they got it right.
与抽象类相比有何优势?除了您可以实现多个接口但只能扩展一个(抽象或非抽象)类这一事实之外,它与抽象类相同,它的所有方法都是抽象和公共的
Advantages over an abstract class? except the fact you can implement multiple interfaces but extend only one (abstract or not) class, it's the same as an abstract class that all of it's methods are abstract and public
接口允许 Java 中的主格类型跨不相交类层次结构。
这是由于类层次结构上的“单一继承”限制造成的,并且“封闭类型”。我将对 子类型多态性 以及它的 Java 实现保持沉默;-)
还有其他解决方案这个问题例如动态类型、结构类型、多重继承和特征等。每种方法都有优点和缺点。接口正是 Java 采用的方法。
Interfaces allow the nominative typing in Java to work across disjoint class hierarchies.
This is due to the "single inheritance" limitation on a class hierarchy and "closed types". I will hold my tongue on subtype polymorphism and Java's implementation of it ;-)
There are other solutions to this problem such as dynamic typing, structural typing, multiple inheritance, and traits, etc. Each approach has advantages and dis-advantages. Interfaces were just the approach that Java took.
Java接口
- 提供
数据封装
,这意味着方法的实现是不可见的。扩展此接口的类必须实现其中声明的所有方法。更多信息:wiki 答案
Java interface
- provides the
data encapsulation
which means, implementation of the methods can not be seen. The class which extends this interface must implement all the methods declared in it.more info: wiki answer