接口和抽象类 [英] interface and abstract class

查看:66
本文介绍了接口和抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有抽象类有所有方法摘要和接口有一些方法,那么这个类和这个接口之间的区别是什么

if we have abstract class having all method abstract and an interface having some method ,then what is the difference between this class and this interface

推荐答案

差别没有不一定适用于该类级别 - 它适用于继承它的类的级别。如果你有一个完全抽象的基类,那么你的派生类只能派生自那个类,它不能从任何其他类派生。使用接口,您的类可以实现多个接口以及从另一个类派生。



但是,如果您的抽象类包含抽象属性/方法,但也提供字段,派生类可以访问这些字段 - 接口不能提供字段。此外,你的抽象类可能提供必须继承的构造函数 - 再次,你不能用接口做的事情。
The difference doesn't necessarily apply at that class level - it applies at the level of the class that inherits it. If you have a totally abstract base class then your derived class can only derive from that one class, it cannot derive from any others. With an interface, your class can implement multiple interfaces as well as being derived from another class.

If, however, your abstract class contains abstract properties/methods, but also provides fields, your derived classes get access to these fields - interfaces cannot provide fields. Also, your abstract class may provide constructors which must be inherited - again, something you can't do with an interface.


你所说的是一个糟糕的编码,因为C#只支持那么单继承,如果你编写一个没有实现没有方法的抽象类,你就不能从那个抽象类和另一个类继承。
What you are talking about is a bad coding pratique because C# only support single inheritance then, if you write an abstract class without implement no methods on that, you will be not able to inherit from that abstract class and from another class together.


这篇关于接口和抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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