接口继承和实现继承之间的区别 [英] difference between interface inheritance and implementation inheritance
问题描述
我在Meyers的书中找到了这两个术语,但有什么区别?
I found those two terms in the book of Meyers, but what is the difference?
推荐答案
接口继承是公共继承,而实现继承是私有继承。
Interface inheritance is public inheritance, while implementation inheritance is private inheritance.
如果B类公开继承自A,B 是一个 A:它继承了A的整个接口,并且B对象的(引用/指针)B对象可以被自动地上转到A,并且在期望A的对象的任何地方使用。然而,如果B私有地从A继承,则B 是在中实现的。A:只有A的实现被继承,而不是它的接口。因此(引用/指向)B对象可以在预期A对象的地方使用。
If class B publicly inherits from A, B is an A: it inherits the whole interface of A, and a (reference/pointer to) a B object can be automatically be upcasted to A, and used wherever an object of A is expected. However, if B privately inherits from A, B is-implemented-in-terms-of A: only the implementation of A is inherited, not its interface. Thus (references/pointers to) B objects can not be used in places where A objects are expected.
为了反映@ Michal的评论,这里有一些链接(主要基于 googlingc ++实现继承)来演示这些术语在C ++语境中的常见用法:
To reflect on @Michal's comment, here are some links (based largely on googling "c++ implementation inheritance") to demonstrate the common usage of these terms in the context of C++:
- C++ Design/Coding tips - Part 7
- Interfaces
- Uses and Abuses of Inheritance, Part 1
这篇关于接口继承和实现继承之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!