abstract class和interface在.NET之间的区别是什么?区别、class、abstract、NET

由网友(心酸的情歌∽)分享简介:这是抽象类可以从其他的类继承,并要求人们超越抽象函数,抽象性等。An abstract class can be inherited by another class, and require people to override the abstract functions, abstract properties...

这是抽象类可以从其他的类继承,并要求人们超越抽象函数,抽象性等。

An abstract class can be inherited by another class, and require people to override the abstract functions, abstract properties, etc.

这是接口,也可以用其他类来实现,也需要人来实现的功能,属性,索引等。

An interface also can be implemented by another class, also require people to implement the functions, properties, indexers, etc.

唯一的不同,我发现是Visual Studio是足够聪明的自动生成该接口的必要成员。

The only different I found is Visual Studio is smart enough to auto-generate the necessary members of this interface.

那么,有什么不同?

谢谢你〜

推荐答案

在.NET中,类仅限于单继承,但可以实现任意数量的接口。如果从一个抽象类继承,这就是你可以用唯一的一个。它可能不是一个大问题,但它会prevent你也许自MarshalByRefObject继承下来的道路,作为一个例子。

In .NET, classes are limited to single inheritance but can implement any number of interfaces. If you inherit from an abstract class, that's the only one you can use. It may not be a big deal, but it would prevent you from perhaps inheriting from MarshalByRefObject down the road, as an example.

另外,通过一个抽象类定义的行为被限制为从其继承的类。另一方面,通过接口(IDisposable的,IPrintable等)定义行为可以在类层次结构被应用

Also, the behavior defined by an abstract class is limited to the classes that inherit from it. On the other hand, behaviors defined by interfaces (IDisposable, IPrintable, etc.) can be applied across class hierarchies.

阅读全文

相关推荐

最新文章