接口与抽象类 [英] Interfaces vs. abstract classes

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

问题描述

接口VS Base类

在C#中,什么时候应该使用接口以及何时应该使用抽象类?什么可以决定的因素?


In C#, when should you use interfaces and when should you use abstract classes? What can be the deciding factor?

推荐答案

一个抽象类的优点是:


  • 能够指定的方法的默认实现

  • 新增不变检查,以功能

  • 您可以在接口的方法的调用方式稍微控制

  • 有能力提供行为有关或无关的接口免费

接口仅仅是数据传递合同,不具有这些功能。然而,它们通常更灵活作为类型只能从一个类导出,但可以实现任意数量的接口

Interfaces are merely data passing contracts and do not have these features. However, they are typically more flexible as a type can only be derived from one class, but can implement any number of interfaces.

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

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