Java中的抽象类和接口

发布于 2024-10-21 07:19:22 字数 224 浏览 5 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入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

稍尽春風 2024-10-28 07:19:23

用简单的英语来说,接口是一个类,其中所有方法都是抽象的但不可实现(在接口中)。只有这些接口的子类(不是抽象类)必须实现抽象方法。

抽象类有一些方法实现,但可以包含必须由具体子类实现的抽象方法。

维基百科指出(接口)

在面向对象语言中,术语
“接口”经常被用来定义一个
不包含数据的抽象类型
但暴露的行为定义为
方法。一个类拥有所有
对应的方法
据说接口实现了
界面。此外,一个类可以
实现多个接口,并且
因此可以有不同的类型
同一时间。

维基百科:(抽象类)

抽象类或抽象基
类(ABC),是一个不能被
实例化。这样的班级只有
如果语言支持有意义
遗产。一个抽象类是
仅设计为父类
可以派生哪些子类。
抽象类经常被用来
代表抽象概念或
实体。不完整的特征
然后抽象类被共享
一组子类,其中添加
失踪的不同变体
件。

在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):

In object-oriented languages the term
"interface" is often used to define an
abstract type that contains no data
but exposes behaviors defined as
methods. A class having all the
methods corresponding to that
interface is said to implement that
interface. Furthermore, a class can
implement multiple interfaces, and
hence can be of different types at the
same time.

Wikipedia: (Abstract Class)

An abstract class, or abstract base
class (ABC), is a class that cannot be
instantiated. Such a class is only
meaningful if the language supports
inheritance. An abstract class is
designed only as a parent class from
which child classes may be derived.
Abstract classes are often used to
represent abstract concepts or
entities. The incomplete features of
the abstract class are then shared by
a group of subclasses which add
different variations of the missing
pieces.

In java you extend a class/abstract class but you implement an interface.

寂寞笑我太脆弱 2024-10-28 07:19:22

使用“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.

甜尕妞 2024-10-28 07:19:22

如果您从技术角度较少地看待它,但您可以或应该如何使用它:

接口的主要优点是一个类可以实现任意多个接口。与此相反,一个类只能扩展一个其他类。 (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 ....

赏烟花じ飞满天 2024-10-28 07:19:22

接口不包含任何实现。它只是描述了实现接口的类如何与其他类交互。

抽象类可以包含一些方法,这些方法被实现并定义抽象方法,类似于接口。

类和接口的使用不应考虑整个项目,而应考虑特定的地方。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文