实现接口的类和该接口之间有什么关系吗?

发布于 2024-07-25 12:10:27 字数 355 浏览 7 评论 0原文

考虑这样的类层次结构:

  • Book extends Goods
  • Book Implements Taxable

正如我们所知,子类与其超类之间存在关系 (is-a)。

问:BookTaxable 之间是否存在类似于“is-a”的关系?

很好的答案,但你说“is-a”也是 BookTaxable 之间的关系,but“is-a”是之间的关系,并且接口不是类!

Consider this class hierarchy:

  • Book extends Goods
  • Book implements Taxable

As we know, there is a relationship between a subclass and its superclass (is-a).

Q: Is there any relationship like "is-a" between Book and Taxable?

GOOD Answers, but you said that "is-a" is also a relationship between Book and Taxable, but "is-a" is a relation between classes, and an interface is not a class!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

痴骨ら 2024-08-01 12:10:27

是的。 这种关系是完全相同的

书也是应纳税的。

编辑

接口是一个恰好与 Java(可能还有我不知道的 C#)interface 关键字匹配的工件。

在 OO 中,接口是类“提交”执行的一组操作,仅此而已。 就像对象类与其客户之间的契约一样。

没有interface关键字的面向对象编程语言,仍然具有类接口面向对象概念。

Yes. The relationship is exactly the same

Book is a Taxable too.

EDIT

An interface is an artifact that happens to match Java's ( and probably C# I don't know ) interface keyword.

In OO interface is the set of operations that a class is 'committed' perform and nothing more. Is like a contract between the object class and its clients.

OO programming languages whose don't have interface keyword, still have class interface OO concept.

老子叫无熙 2024-08-01 12:10:27

嗯,有“支持操作”。 就我个人而言,我不认为“is-a”、“can-do”等助记符非常有用。 我更喜欢考虑类型允许的内容,无论它们是专门化现有行为还是实现行为本身等等。类比,如抽象,往往是有漏洞的。 如果您知道接口继承和实现继承之间的区别,您可能不需要任何额外的措辞来表达它。

Well there's "supports-the-operations-of". Personally I don't find the "is-a", "can-do" etc mnemonics to be terribly useful. I prefer to think in terms of what the types allow, whether they're specialising existing behaviour or implementing the behaviour themselves etc. Analogies, like abstractions, tend to be leaky. If you know what the different between interface inheritance and implementation inheritance is, you probably don't need any extra phraseology to express it.

花间憩 2024-08-01 12:10:27

“表现得像……”

这就是我想说的。 不是某物,而是表现得像某物。 或者作为替代“可以做某事”,但这比行为更具体。

"Behaves like..."

That's what what I would say. Not is something, but behaves like something. Or as an alternative "can something", but that's more specific than behaviour.

几度春秋 2024-08-01 12:10:27

关系如下所述:'实现'

这些关系名称源自句子中的使用。 “Book 'is-a' Goods”可以不用引号和连字符来写,这是有道理的。 同样,Book 'implements' Taxable 也可以不带引号编写。

the relationship would be as stated: 'implements'

these relationship names spring from usage in sentences. "Book 'is-a' Goods" can be written without the quotes and hyphen and it makes sense. similarly, Book 'implements' Taxable can be written without the quotes.

深巷少女 2024-08-01 12:10:27

当我们说一个类扩展另一个类时,它具有很强的关系,称为“继承”。这意味着当一个子类扩展父类时,子类应该能够从父类继承一些东西,比如马是一种动物。马继承了但是当一个类实现另一个类时,子类试图实现一个契约,不需要从父类继承任何东西,只需遵循一个契约,这就是为什么接口的所有方法默认都是抽象的,但你可以在类中提供一些具体的方法(供子类继承)并且可以将其中的一些抽象化就好了。

所以对我来说,扩展是实现契约中的继承和接口。希望这是令人满意的

When we say one class extends another class it is having strong relation ship known as 'inheritance'.this means when one child extends parent then child should be able to inherit something from parent class like horse IS A animal .Horse is inheriting some properties of animal .But when a class implements another class then child class is trying to implement a contract dosen't need to inherit anything from parent just following a contract ,that why interface all methods are abstract by default but you can provide some concrete method in class(for child class to inherit) and can make some of then abstract is well .

So for me extends is inheritance and interface in implementing contract.hope this is satisfactory

薄情伤 2024-08-01 12:10:27

应该这样做:

public static boolean implementsInterface(Object object, Class interf){
    return interf.isInstance(object);
}

例如,

 java.io.Serializable.class.isInstance("a test string")

评估结果为 true。

来自:测试对象是否实现接口

This should do:

public static boolean implementsInterface(Object object, Class interf){
    return interf.isInstance(object);
}

For example,

 java.io.Serializable.class.isInstance("a test string")

evaluates to true.

from: Test if object implements interface

孤芳又自赏 2024-08-01 12:10:27

到底是什么令人兴奋? 多个问号和多个感叹号?

尽管 Taxable 是一个接口,但我们可以说一本书是应税的,您是否对此感到困扰? 请冷静下来。

对于类与接口和超类的关系,语言中有不同的关键字,但这种关系的概念性质是相同的,因此使用相同的英语术语来描述它是完全合理的。 一本书是应税的,就像一本书是一件商品一样。 为了使术语更接近,书籍是应税项目。 没关系。

What's all the excitement about? The multiple question marks and multiple exclamation points?

Does it bother you that we can say a Book is Taxable, even though Taxable is an interface? Please calm down.

There are different keywords in the language for a class' relationship to an interface and to a superclass, but the conceptual nature of that relationship is the same, therefore it's entirely reasonable to use the same English terms to describe it. A Book is Taxable, just as a Book is a Good. To bring the terms even closer, a Book is a TaxableItem. It's OK.

梦旅人picnic 2024-08-01 12:10:27
Book implements Taxable

这里 BookTaxable 之间的关系是

书籍“是”应税的

参考此。 你可以看到它说

当我们谈论继承时,最常用的关键字是extendsimplements。 这些词将确定一个对象是否是另一个对象的IS-A类型。

Book implements Taxable

Here also the relationship between Book and Taxable is

Book 'is a' Taxable

Refer this. You can see it says

When we talk about inheritance the most commonly used keyword would be extends and implements. These words would determine whether one object IS-A type of another.

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