实现接口的类和该接口之间有什么关系吗?
考虑这样的类层次结构:
Book extends Goods
Book Implements Taxable
正如我们所知,子类与其超类之间存在关系 (is-a)。
问:Book
和 Taxable
之间是否存在类似于“is-a”的关系?
很好的答案,但你说“is-a”也是 Book
和 Taxable
之间的关系,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的。 这种关系是完全相同的
书也是应纳税的。
编辑
接口是一个恰好与 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.嗯,有“支持操作”。 就我个人而言,我不认为“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.
“表现得像……”
这就是我想说的。 不是某物,而是表现得像某物。 或者作为替代“可以做某事”,但这比行为更具体。
"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.
关系如下所述:'实现'
这些关系名称源自句子中的使用。 “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.
当我们说一个类扩展另一个类时,它具有很强的关系,称为“继承”。这意味着当一个子类扩展父类时,子类应该能够从父类继承一些东西,比如马是一种动物。马继承了但是当一个类实现另一个类时,子类试图实现一个契约,不需要从父类继承任何东西,只需遵循一个契约,这就是为什么接口的所有方法默认都是抽象的,但你可以在类中提供一些具体的方法(供子类继承)并且可以将其中的一些抽象化就好了。
所以对我来说,扩展是实现契约中的继承和接口。希望这是令人满意的
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
应该这样做:
例如,
评估结果为 true。
来自:测试对象是否实现接口
This should do:
For example,
evaluates to true.
from: Test if object implements interface
到底是什么令人兴奋? 多个问号和多个感叹号?
尽管 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.
这里
Book
和Taxable
之间的关系是参考此。 你可以看到它说
Here also the relationship between
Book
andTaxable
isRefer this. You can see it says