实现接口而不继承它 [英] Implementing an interface without inheriting it

查看:109
本文介绍了实现接口而不继承它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承特定接口的类。该接口包含一个方法,该方法在类中实现。一切都好。

但是如果我的类具有相同的方法但没有继承接口会发生什么?我认为它不能正常工作,但为什么呢?



谢谢,

David

I have a class that inherits a specific interface. The interface contains one method, which is implemented in the class. Everything is OK.
But what happens if my class has the same method, but without inheriting the interface? I assume that it will not work properly, but why?

Thanks,
David

推荐答案

请参阅我对该问题的评论。这不是一个好问题,但除了解决方案1之外,我会尝试解释一些问题。



当你问同样的方法时,我的问题会相同作为...什么方法?当你说无法正常工作时,我的问题是:什么是正确的?



让我们假设继承你的意思是添加一个接口到一个实现类型的继承列表。如果你这样做,相同的方法将意味着与该接口的一种或另一种方法相同。如果没有提到的接口名称,则表示没有相同。您可能有一些普通方法,虚拟或非虚拟方法,与任何其他类型,接口无关。没有什么可以正常工作。



你只需要了解接口的作用。解决方案1可以帮助您理解这一点。我想补充一点,接口的使用类似于使用抽象类作为基类,并且与后期绑定多态有关。首先,有很多不同之处,多重继承以及在struct中实现接口的能力,这是一种值类型,而不是引用类型。我之所以提到抽象类只是因为它们可以帮助你理解界面,但是更多细节请看我过去的答案:

为什么我们需要为接口创建实例而不是实现? [ ^ ],

对接口的怀疑 [ ^ ],

如何决定选择抽象类或接口 [ ^ ],

抽象类和接口之间的差异,如果它们具有相同的no方法和var [ ^ ],

我们使用时抽象,当我们使用界面......? [ ^ ],

多态性没有过载和超载是可能的 [ ^ ],

接口和多态性 [ ^ ]。



对不起,很多事情在那些重复答案,但我真的建议仔细查看所有这些。



-SA
Please see my comment to the question. It's not a good question, but I'll try to explain something in addition to Solution 1.

When you ask "same method", my question would be "same as… what method?" When you say "will not work properly", my question would be: "what is properly?"

Let's assume by "inheriting" you mean adding an interface to an inheritance list of an implementing type. If you do it, "same" method would mean "same as one or another method of this interface". If there is no interface name mentioned, you don't have "same". You may have some ordinary method, virtual or not, which has nothing to do to any other type, interface or not. There is nothing to "work properly".

You just need to understand what interfaces do. Solution 1 may help you to understand that. I would add that the use of interfaces is similar to the use of an abstract class as a base class and is related to late binding and polymorphism. There are a lot of differences, first of all, multiple inheritance and the ability to implement an interface in struct, which is a value type, not a reference type. I mentioned abstract classes only because they may help you to understand interfaces, but for more detail, please see my past answers:
Why we need to create instance for interface not for implementation?[^],
Doubts on Interfaces[^],
How to decide to choose Abstract class or an Interface[^],
Difference between abstract class and interface if they have same no of methods and var[^],
When we use abstract and when we use interface...?[^],
POLYMORPHISM WITHOUT OVERLOADING AND OVERRRIDING IS POSSIBLE[^],
Interfaces and Polymorphism[^].

Sorry, a lot of matter is repeated in those answers, but I really advise to look through all of them.

—SA


如果我理解你的问题正确一个类可以有一个具有特定名称的方法,即使它没有实现定义这样名称的接口。



接口的想法是在不知道确切类的情况下向类的用户提供诸如方法或属性的名称之类的信息。



例如,接口可以是 ICar ,它将定义 OpenDoor 方法。法拉利和保时捷都会实施这种方法,因此即使不知道汽车是法拉利还是保时捷,汽车用户也可以拨打 OpenDoor 方法。但是,一个名为 House 的类可以实现 OpenDoor 方法而没有任何问题,但代码依赖于 ICar 接口不知道房子有相同的方法,因为房子不会实现 ICar
If I understand your question correctly a class can have a method with a specific name even if it does not implement an interface that defines such name.

The idea of the interface is to provide information such as names of the methods or properties to the user of a class without knowing the exact class.

For example an interface could be ICar which would define OpenDoor method. Both Ferrari and Porsche would implement the method so the user of the car could call OpenDoor method even without knowing if the car is Ferrari or Porsche. However, also a class called House could implement OpenDoor method without any problems but the code relying on ICar interface would not know that the house has the same method since house would not implement ICar.


这篇关于实现接口而不继承它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆