接口和抽象类有什么区别?
答
它们非常相似,但存在一些重要的技术差异:
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.