Java中的抽象类和接口
可能的重复:
Java [接口/抽象类]的使用
对于java新手来说,什么是在项目中使用抽象类和接口之间的区别?
Possible Duplicate:
Use of Java [Interfaces / Abstract classes]
Being new to java, what are the differences between using an abstract class in your project and an interface ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
接口是一个契约(没有实现),其中抽象类既是一个契约,又是一个实现。
http:// /www.java-tips.org/java-se-tips/java.lang/difference- Between-abstract-classes-and-inter.html
A interface is a contract (no implementation), where an abstract class is both a contract WITH implementation.
http://www.java-tips.org/java-se-tips/java.lang/difference-between-abstract-classes-and-inter.html
用简单的英语来说,接口是一个类,其中所有方法都是抽象的但不可实现(在接口中)。只有这些接口的子类(不是抽象类)必须实现抽象方法。
抽象类有一些方法实现,但可以包含必须由具体子类实现的抽象方法。
维基百科指出(接口):
维基百科:(抽象类)
在java中,你扩展一个类/抽象类,但你实现一个接口。
In simple engilsh, an interface is a class where all methods are abstract but not implementable (in the interface). Only subclasses (which are not abstract classes) of those interface must implement the abstract method.
Abstract classes have some method implementations, but can contain abstract methods that must be implemented by concrete subclasses.
Wikipedia states (interface):
Wikipedia: (Abstract Class)
In java you extend a class/abstract class but you implement an interface.
使用“extends”关键字一次只能继承一个类,但可以使用“implements”关键字实现任意多个接口。
此外,抽象类可以同时具有抽象和具体(实现)方法以及变量。
You can only inherit from one class at a time, using the "extends" keyword, but you can implement as many interfaces as you want, using the "implements" keyword.
Also, abstract classes can have both abstract and concrete (implemented) methods, as well as variables.
如果您从技术角度较少地看待它,但您可以或应该如何使用它:
接口的主要优点是一个类可以实现任意多个接口。与此相反,一个类只能扩展一个其他类。 (java中没有多重继承)。
通过使用接口,您可以向您的类添加单个“功能”。因此,您经常会看到接口名称以“able”结尾。例如“可序列化”或“可解析”或类似的东西。
抽象类可以是通用类,如果强制扩展的话。例如“车辆”。你不能使用“车辆”本身,因为不存在只有“车辆”的东西。所以你必须实现一个扩展该类的类,可以是汽车或船......
If you look at it less technically but how you can or should use it:
The main Advantage of an interface is that a class can implement as many interfaces as you like. In Contrast to that one class can only extend one single other class. (There is no multiple inheritance in java).
With using interfaces you can add single "Capabilities" to your classes. Therefor you will often read that interfaces names ends with "able". like "Serializable" or "Parceable" or something like that.
An Abstract class can be a general class which if forced to be extended. Like a "Vehicle" for example. You can't use a "Vehicle" itsself because there is no thing existing which is only a "Vehicle". So you have to implement a class extending that class which could be Cars or Boats ....
接口不包含任何实现。它只是描述了实现接口的类如何与其他类交互。
抽象类可以包含一些方法,这些方法被实现并定义抽象方法,类似于接口。
类和接口的使用不应考虑整个项目,而应考虑特定的地方。
Interface doesn't contain any implementation. It just describes how the class, which implements the interface, can interact with other classes.
Abstract class can contain some methods, which are implemented and define abstract methods, similiar to interfaces.
The usage of classes and interfaces should be considered not to the whole project, but to particular places.