有时毫无疑问,但有时这可能是一个非常艰难的决定。让我给你看几个例子:
遗产:
public class Employee{
public void goToWork(){
//...
}
}
public class Lawyer extends Employee{
//private Employee body;
/*public void goToWork(){
body.goToWork(); // Does that make any sense?
}*/
}
代表团:
public class Lights{
public void turnOn(){
//...
}
}
public class Car{ //public class Car extends Lights? Nice car then...
private Lights light;
public void turnOnLights(){
light.turnOn();
}
}
哪一个?
public class Field{
public void grow(Seeds s){
//...
}
}
public class Plantation{ //extends Field{ makes sense too
private Field field;
private Worker[] workers;
private Vehicle[] tractors;
public void grow(Seeds s){
//...
field.grow(s);
}
}
那么,在继承和委托两者都可以接受并且各有优缺点的情况下,是否有任何提示可以帮助在继承和委托之间进行选择?选择是随机的并且完全基于程序员的心情吗?