C# 抽象类和接口有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15178219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:21:47  来源:igfitidea点击:

What's the difference between an abstract class and an interface?

c#oopinterfaceabstract-class

提问by WpfBee

Suppose we have two methods M1()and M2()in an interface. An abstract class also has just the same two abstract methods. If any class implemented this interface or inherited from the abstract class, it will have to implement both the methods in it.

假设我们有两个方法M1()M2()一个接口。抽象类也有相同的两个抽象方法。如果任何类实现了此接口或从抽象类继承,则必须实现其中的两个方法。

So to me, it seems that an interface or an abstract class behaves the same for my scenario. So, can anyone highlight the difference between these two in this specific caseand suggest whether to use an abstract class or an interface here?

所以对我来说,接口或抽象类在我的场景中的行为似乎是一样的。那么,任何人都可以在这种特定情况下强调这两者之间的区别,并建议在这里使用抽象类还是接口?

采纳答案by Matthew

There are technical differences between Abstract Classes and Interfaces, that being an Abstract Class can contain implementation of methods, fields, constructors, etc, while an Interface only contains method and property prototypes. A class can implement multiple interfaces, but it can only inherit one class (abstract or otherwise).

抽象类和接口之间存在技术差异,抽象类可以包含方法、字段、构造函数等的实现,而接口仅包含方法和属性原型。一个类可以实现多个接口,但它只能继承一个类(抽象的或其他的)。

However, in my opinion, the most important difference between Interfaces and Abstract Classes is the semantic difference.

但是,在我看来,接口和抽象类之间最重要的区别是语义上的区别。

An Interface defines what something can do(how it behaves), and an Abstract Class defines what something is.

接口定义了某物可以做什么(它的行为方式),而抽象类定义了某物是什么。

Take for example IEnumerable, the semantic meaning behind this is that anything that implements IEnumerableis enumerable, it doesn't mean that it's an enumeration, but that it can behave like one (can be enumerated).

举个例子IEnumerable,这背后的语义是任何实现的IEnumerable都是可枚举的,这并不意味着它是一个枚举,而是它可以表现得像一个(可以被枚举)。

Contrast that with a washing machine example, anything that inherits it is a type of washing machine. Anything that inherits it would be a type of washing machine, a top loader, or side loader, etc.

与洗衣机的例子相比,任何继承它的东西都是一种洗衣机。任何继承它的东西都将是一种洗衣机、顶部装载机或侧装载机等。

Instead, if you had an interface called ICanWash, which could contain a method called Wash. You could have various things implement ICanWash, be it a Person, an abstract washing machine class, etc, where the actual implementation does not matter, just you need to know that the behavior is that it can wash things.

相反,如果您有一个名为 的接口ICanWash,它可以包含一个名为 的方法Wash。你可以有各种各样的东西实现ICanWash,无论是Person一个抽象的洗衣机类等等,实际的实现并不重要,你只需要知道行为是它可以洗东西。

In summary, classes define what something is, interfaces define what something can do.

总之,类定义什么是什么,接口定义什么可以做什么。

回答by CharlesW

Interface allows a class to inherit/implement more than one interface, while in C# you can only inherit from one class.

接口允许一个类继承/实现多个接口,而在 C# 中你只能从一个类继承。

Multiple inheritance, basically.

多重继承,基本上。

回答by omer schleifer

Abstract class not only holds abstract methods, it may also hold other fields and methods with implemntation. In c# you can not inherit from multipule classes, but you can implement multipule interfaces. So the short answer is: whenever possible use interfaces and not abstract classes. In your example it is possibe- and therfor recomended to use an interface.

抽象类不仅包含抽象方法,还可以包含其他具有实现的字段和方法。在 c# 中,您不能从多个类继承,但可以实现多个接口。所以简短的回答是:尽可能使用接口而不是抽象类。在您的示例中,建议使用接口。

回答by Pieter Geerkens

Two quick thoughts on differences between interfaces and abstract classes:

关于接口和抽象类之间差异的两个快速思考:

  1. Abstract classes desired if future expansion is likely, as an abstract class can be expanded, but an interface would have to be enhanced by addition of another interface, I2.
  2. Single (implementation) inheritance means chooses abstract classes careully, to most closely reflect the true basic nature. Interfaces can easly be added to an implemntation, but an abstract class can only be added if there is not one already.
  1. 如果将来可能扩展,则需要抽象类,因为可以扩展抽象类,但必须通过添加另一个接口 I2 来增强接口。
  2. 单一(实现)继承意味着谨慎选择抽象类,以最接近地反映真实的基本性质。接口可以很容易地添加到实现中,但只有在没有抽象类的情况下才能添加抽象类。

回答by Fabio

From MSDN:

MSDN

By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes

例如,通过使用接口,您可以在一个类中包含来自多个源的行为。该功能在 C# 中很重要, 因为该语言不支持类的多重继承

So, use interface if you want that any class can inherit that methods.

因此,如果您希望任何类都可以继承该方法,请使用接口。

From same MSDN page:

从同一个 MSDN 页面:

In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class.

此外,如果要模拟结构的继承,则必须使用接口,因为它们实际上不能从另一个结构或类继承。