接口和抽象类有什么区别? [英] What is the difference between an interface and an abstract class?

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

问题描述


可能重复:

接口与抽象类(一般OO)

我不太清楚差异。

谢谢

推荐答案

它们非常相似,但存在一些重要的技术差异:

They are quite similar but there are some important technical differences:


  • 抽象类允许您为某些方法提供默认实现但是接口不允许你提供任何实现。

  • 你可以实现多个接口,但只能从一个抽象类继承。

这些差异会影响两种技术的使用方式:

These differences affect how the two techniques should be used:


  • 你应该使用一个接口来定义合约

  • 抽象类对重用代码有用...但是请注意,这不是重用代码的唯一方法。您还应该考虑其他方法,例如收容。

  • You should use an interface to define a contract.
  • An abstract class can be useful to reuse code... but be aware that it is not the only way to reuse code. You should also consider other approaches such as containment.

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

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