具有未实现方法的接口和抽象类之间有什么区别?

问题描述:

从很久以后我试着理解为什么界面在使用非实现方法的抽象类时出现了,即抽象方法用于相同的目的?



我有从几篇文章中看到了这么多的差异,但没有人给出上述问题的答案。可以帮我理解一下吗?



我是什么尝试过:



探索了几篇文章来找出它的答案..但是找不到

From long back I am trying understand why interface came into picture when abstract class with non-implemented methods i.e., abstract methods serves the same purpose?

I have seen so many differences from several articles but none of them gave answer for the above question..Can some one please help me to understand it?

What I have tried:

Explored several articles to figure out the answer for it..But not able to find

C#有一个限制:你只能从一个基类继承,而不能从两个或更多基类继承。

但如果你想为你的两个不同的行为添加两个不同的行为会给你一个问题class:也许它是一个Person实例,也充当属性集合。如果没有实现Person以允许属性,那么你不能在同一个类中使用它们。

输入接口:它不是一个类,它是一个合同。界面说你可以加入这个俱乐部,只要你遵守规则 - 规则是你必须实现这些属性和方法。一旦你这样做,你就可以获得俱乐部会员资格的所有好处,但你仍然可以从不同的基类派生你的班级。

你可以构建一个看起来像接口的抽象类,但不能反过来 - 并且你不能从两个抽象基类派生你的类。
C# has a restriction: you can only inherit from one base class, never two or more.
But that give you a problem if you want to add two different behaviors to your class: perhaps it's a Person instance that also acts as a Collection of Attributes. If Person isn't implemented to allow for Attributes at all then you can't use both within teh same class.
Enter the Interface: it's not a class, it's a contract. An Interface says "You can join this club, provided you obey the rules" - and the rules are "you must implement these properties and methods". Once you do, you gain all the benefits of club membership, but you can still derive your class from a different base class.
You can construct an Abstract class that looks like an Interface, but not vice versa - and you cannot derive your class from two Abstract base classes.


优点和缺点...



界面:

*一个对象可以实现多个接口,因此可以用不同的方式伪装自己。

*接口不能保存常见的实现,所以每个类必须有自己的。

抽象类:

*在C#中你只能从一个继承,所以不能通过继承支持乘法行为

*更严格的是什么(和如何)实现的东西

*可以保持常见的实现
Pros and cons...

Interface:
* An object can implement more than one interfaces, so can disguise itself in different ways.
* Interface can not hold common implementations, so every class must have its own.
Abstract Class:
* In C# you can inherit only from one, so multiply behaviors are not supported via inheritance
* More strict about what (and how) to implement things
* Can hold common implementations