abstract class和interface在.NET之间的区别是什么? [英] What is the difference between abstract class and interface in .NET?

查看:190
本文介绍了abstract class和interface在.NET之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于abstract class和interface在.NET之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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