gpt4 book ai didi

java - 没有任何实现和变量的抽象类是否有效接口(interface)?

转载 作者:行者123 更新时间:2023-12-04 09:24:05 24 4
gpt4 key购买 nike

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .

1年前关闭。




Improve this question




我正在回顾 OOP 的概念,阅读 .
这里本书将接口(interface)定义为

The set of all signatures defined by an object’s operations is called the interface to the object. (p.39)


抽象类为

An abstract class is one whose main purpose is to define a common interface for its subclasses. An abstract class will defer some or all of its implementation to operations defined in subclasses; hence an abstract class cannot be instantiated. The operations that an abstract class declares but doesn’t implement are called abstract operations. Classes that aren’t abstract are called concrete classes. (p.43)


而且我想知道,如果我定义一个没有任何内部数据(变量)和具体操作的抽象类,只是一些抽象操作,它实际上不只是一组签名吗?不就是一个接口(interface)吗?
所以这是我的第一个问题:
  • 我可以说只有抽象函数的抽象类是“ 实际上(或理论上)”的接口(interface)吗?

  • 然后我想,这本书还谈到了类型和类。

    An object’s class defines how the object is implemented. The class defines the object’s internal state and the implementation of its operations. In contrast, an object’s type only refers to its interface—the set of requests to which it can respond. An object can have many types, and objects of different classes can have the same type. (p.44)


    然后我记得有些语言,比如Java,不允许多重继承,但它允许多重实现。所以我猜对于某些语言(如Java),只有抽象操作的抽象类!=接口(interface)。
    所以这是我的第二个问题:
  • 我可以说只有抽象函数的抽象类是“ 通常等同于 ”的语言中支持多重继承的接口(interface)吗?

  • 我的第一个问题是检查定义,第二个问题是关于其他语言的工作方式。我主要使用 Java 和 Kotlin,所以我不太确定其他支持多重继承的语言。我不希望对当前的 OOP 语言进行一般、全面的审查,但对单一语言(也许是 python?)的一点提示会非常有帮助。

    最佳答案

  • 号码

  • 在 Java 中,每个类都是 Object 的子类,所以你不能只用抽象方法创建一个抽象类。它将始终具有从 Object 继承的方法实现。 : hashCode() , equals() , toString() , ETC。
  • 是的,差不多。

  • 例如,在 C++ 中,没有特定的 interface关键字,接口(interface)只是一个没有实现的类。 C++ 中没有通用的基类,所以你真的可以创建一个没有实现的类。
    多重继承并不是真正的决定性特征。 Java 有一种多重继承,具有称为“接口(interface)”的特殊类,甚至可以有 default。方法。
    它是真正的通用基类 Object这会有所不同。 interface是你创建一个不从 Object 继承的类的方式.

    关于java - 没有任何实现和变量的抽象类是否有效接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63050082/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com