java中的composition和inheritance的区别
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中的composition和inheritance的区别相关的知识,希望对你有一定的参考价值。
在看THINKING IN JAVA时候。书中讲了composition和inheritance。这两个的区别在哪里没有看的很明白。哪位大侠指点下,最好举个例子。谢谢哦。
他们两者的能力几乎是一样的。主要区别就是在 你所需表达的概念上 。如果你需要的只是现有类的某种能力,而不是该类的接口,也就是说你只是需要现有类来做某种事情,而你创建的类与该类并没有类似的接口的话,你就应该使用 composition 。而 inheritance 的应用正好相反。也就是说你需要的是现有类的接口,也就是你希望在可以使用现有类的地方,也可以使用你新创建的类,那么这是你就应该是用 inheritance. Inheritance 的另外一个好处就是子类可以 upcast 到基类。 upcast 的作用就是在所有使用父类的地方你都可以使用子类(除非你使用的是 private 继承) 。这是因为子类继承了父类中的所有成员(除了一些特殊情况)。所以另外一个决定是使用 composition 或 inheritance 的方式就是问一下问题:你是否需要从新建的类 upcast 。 参考技术A Composition vs. InheritanceObject composition and inheritance are two techniques for reusing functionality in object-oriented systems [7]. Class inheritance, or subclassing, allows a subclass' implementation to be defined in terms of the parent class' implementation. This type of reuse is often called white-box reuse. This term refers to the fact that with inheritance, the parent class implementation is often visible to the subclasses.
Object composition is a different method of reusing functionality. Objects are composed to achieve more complex functionality. This approach requires that the objects have well-defined interfaces since the internals of the objects are unknown. Because objects are treated only as "black boxes," this type of reuse is often called black-box reuse.
Each of these two methods have advantages and disadvantages. The advantage of class inheritance is that it is done statically at compile-time and is easy to use. The disadvantage of class inheritance is that the subclass becomes dependent on the parent class implementation. This makes it harder to reuse the subclass, especially if part of the inherited implementation is no longer desirable. According to [16], "inheritance breaks encapsulation." One way around this problem is to only inherit from abstract classes. Another problem with class inheritance is that the implementation inherited from a parent class cannot be changed at run-time.
In object composition, functionality is acquired dynamically at run-time by objects collecting references to other objects. The advantage of this approach is that implementations can be replaced at run-time. This is possible because objects are accessed only through their interfaces, so one object can be replaced with another just as long as they have the same type. In addition, since each object is defined in terms of object interfaces, there are less implementation dependencies [7]. The disadvantage of object composition is that the behavior of the system may be harder to understand just by looking at the source code. A system using object composition may be very dynamic in nature so it may require running the system to get a deeper understanding of how the different objects cooperate.
In general, object composition should be favored over inheritance. It promotes smaller, more focused classes and smaller inheritance hierarchies. Most designers overuse inheritance, resulting in large inheritance hierarchies that can become hard to deal with. A design based on object composition will have less classes, but more objects. The behavior of such a system depends more on the interrelationships between objects rather then being defined in a particular class [7].
However, inheritance is still necessary. You cannot always get all the necessary functionality by assembling existing components. This is because "the set of components is never quite rich enough in practice" [7]. Inheritance can be used to create new components that can be composed with old components. As a result, the two methods work together.
Due to the flexibility and power of object composition, most design patterns emphasize object composition over inheritance whenever it is possible. Many times, a design pattern shows a clever way of solving a common problem through the use of object composition rather then a standard, less flexible, inheritance based solution. 参考技术B 建议楼主看下Effective Java中的某条目,条目名是组合优先于继承.讲得很清楚明白...
都是复用的技术,但是各有优势..
参考资料:http://blog.csdn.net/zjliu1984/archive/2009/06/26/4299657.aspx
本回答被提问者采纳以上是关于java中的composition和inheritance的区别的主要内容,如果未能解决你的问题,请参考以下文章
python 学习笔记 -- OOP在实例中学习继承(inheritance)和组合(composition)