取自《think in java》第三版
composition vs. inheritance
Both composition and inheritance allow you to place subobjects inside your new class (composition explicitly does this; with inheritance it’s implicit). You might wonder about the difference between the two, and when to choose one over the other.
组合和继承都可以实现代码复用(组合是显式的,继承是隐式的),但这两者的区别是什么?在什么情况下使用组合?什么情况下使用继承?
Composition is generally used when you want the features of an existing class inside your new class, but not its interface. That is, you embed an object so that you can use it to implement functionality in your new class, but the user of your new class sees the interface you’ve defined for the new class rather than the interface from the embedded object. For this effect, you embed private objects of existing classes inside your new class.
通常情况下,当一个类的功能比较单一,你想基于该类,并联合其他的类,完成一项工作时,使用组合。使用private关键字,可以将被组合的对象的接口隐藏起来。
Sometimes it makes sense to allow the class user to directly access the composition of your new class; that is, to make the member objects public. The member objects use implementation hiding themselves, so this is a safe thing to do. When the user knows you’re assembling a bunch of parts, it makes the interface easier to understand. A car object is a good example:
但是,有些时候,使用public让客户端程序员直接访问被组合的对象,这样他们就可以知道你这个功能使用了哪些部分,并且更容易理解。例如一辆车的例子:
//: c06:Car.java
// Composition with public objects.
class Engine {
public void start() {}
public void rev() {}
public void stop() {}
}
class Wheel {
public void inflate(int psi) {}
}
class Window {
public void rollup() {}
public void rolldown() {}
}
class Door {
public Window window = new Window();
public void open() {}
public void close() {}
}
public class Car {
public Engine engine = new Engine();
public Wheel[] wheel = new Wheel[4];
public Door
left = new Door(),
right = new Door(); // 2-door
public Car() {
for(int i = 0; i < 4; i++)
wheel[i] = new Wheel();
}
public static void main(String[] args) {
Car car = new Car();
car.left.window.rollup();
car.wheel[0].inflate(72);
}
} ///:~
Because in this case the composition of a car is part of the analysis of the problem (and not simply part of the underlying design), making the members public assists the client programmer’s understanding of how to use the class and requires less code complexity for the creator of the class. However, keep in mind that this is a special case and that in general you should make fields private.
在这个例子中,将汽车的各个部分设置为public,可以让客户端程序员了解汽车的每个组成部分,并且可以降低汽车类的复杂度——不必再定义被组合类中已有的接口,让客户端程序员直接调用即可。但,这只是个特例,通常情况下,被组合的对象仍然是要设置成private的。
When you inherit, you take an existing class and make a special version of it. In general, this means that you’re taking a general-purpose class and specializing it for a particular need. With a little thought, you’ll see that it would make no sense to compose a car using a vehicle object—a car doesn’t contain a vehicle, it is a vehicle. The is-a relationship is expressed with inheritance, and the has-a relationship is expressed with composition.
继承是“is-a”的关系,而组合则是“has-a”的关系;继承是复用基类,并且创造一个基类的特殊版本,如鸟和凤凰,动物和青蛙一样。并且以上边的例子为例,汽车的父类是交通工具,在逻辑上,交通工具是不能被组合进汽车的,汽车也不必包含一个交通工具。
请大拿们给点建议。
------解决方案--------------------
可以说惨不忍睹.....
最怕中国人翻译国外原版书,整个变了味,老外也看不懂了,中国人也看不懂了
只有他自己知道自己在说什么......