C# 中的抽象类和接口类有什么不同?
C# 中的抽象类和接口类有什么不同?
What is different between an abstract and an Interface class in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
C# 中的抽象类和接口类有什么不同?
What is different between an abstract and an Interface class in C#?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
接口不是类,它只是一个契约,定义了类必须实现的公共成员。
抽象类只是一个不能创建实例的类。通常,您会使用它来定义一个基类,该基类定义了一些要实现的派生类的虚拟方法。
An interface is not a class, it is just a contract that defines the public members that a class must implement.
An abstract class is just a class from which you cannot create an instance. Normally you would use it to define a base class that defines some virtual methods for derived classes to implement.
而不是在这里写下整个内容..
尝试 http://www.codeproject.com/KB/ cs/abstractsvsinterfaces.aspx
Rather than writing whole thing here..
try http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx
一个类可以实现多个接口,但只能继承一个抽象类。
抽象类可以为其方法提供实现。接口不能提供实现。
A class can implement multiple interfaces but can only inherit from one abstract class.
An abstract class can provide implementation for it's methods. An interface cannot provide implementations.
接口的层次高于抽象。
当你设计结构,画uml时,你应该使用接口。
当你实现时,你应该使用抽象来提取重复的东西。
无论如何,不同不仅仅是语法问题..
希望有帮助。
the level of interface is higher than abstract.
when u're design the strcuture, draw the uml, u should use interface.
when u're implement, then u should use abstract to extract repeat things.
anyway, the different is not only a syntax problem..
hope it helps.
谷歌“抽象类与接口”,你会得到很多解释性文章......
Google "abstract class vs interface" and you'll get lots of explanatory articles...
此外,抽象类可能定义了一些函数,但接口不会有任何函数定义,并且派生类必须定义所有函数。
Also, abstract classes may have some functions defined but interfaces will not have any function definition and the deriving class must define all of them.
我将通过用法来解释这一点。当只有一个层次结构时可以使用抽象类,并且没有默认实现;而接口可以跨层次结构(水平)使用,通常称为行为。
接口也是一种抽象,在 C# 中替代了多个类继承,因此这可能会令人困惑,但你必须区分何时使用什么。
希望这有帮助,
罗伯特
I would explain this through the usage. Abstract class can be used when there is only one hierarchy, additionally without default implementation; while interface can be used across hierarchies (horizontally), often referred to as a behavior.
Interface is also an abstraction and in c# substitutes multiple class inheritance, so this may be confusing, but you have to distinguish when to use what.
Hope this helps,
Robert
抽象类的目的是为一组派生类如何工作提供基类定义,然后允许程序员在派生类中填充实现。
当我们创建接口时,我们基本上是在创建一组没有任何必须由已实现的类覆盖的实现的方法。优点是它提供了一种使一个类成为两个类的一部分的方法:一个来自继承层次结构,一个来自接口。
The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.
When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.