我想知道为什么在java构造函数是不继承的?你知道当你上这样的课时:
public class Super {
public Super(ServiceA serviceA, ServiceB serviceB, ServiceC serviceC){
this.serviceA = serviceA;
//etc
}
}
以后当你从Super继承时,java会抱怨没有定义默认构造函数。解决方案显然是这样的:
public class Son extends Super{
public Son(ServiceA serviceA, ServiceB serviceB, ServiceC serviceC){
super(serviceA,serviceB,serviceC);
}
}
这段代码是重复的,不是DRY和无用的(恕我直言)…这又带来了一个问题:
为什么java不支持构造函数继承?不允许这种继承有什么好处吗?
因为构造子类对象的方法可能与构造父类的方法不同。您可能不希望子类的客户端能够调用父类中可用的某些构造函数。
举个愚蠢的例子:
class Super {
protected final Number value;
public Super(Number value){
this.value = value;
}
}
class Sub {
public Sub(){ super(Integer.valueOf(0)); }
void doSomeStuff(){
// We know this.value is an Integer, so it's safe to cast.
doSomethingWithAnInteger((Integer)this.value);
}
}
// Client code:
Sub s = new Sub(Long.valueOf(666L)): // Devilish invocation of Super constructor!
s.doSomeStuff(); // throws ClassCastException
或者更简单:
class Super {
private final String msg;
Super(String msg){
if (msg == null) throw new NullPointerException();
this.msg = msg;
}
}
class Sub {
private final String detail;
Sub(String msg, String detail){
super(msg);
if (detail == null) throw new NullPointerException();
this.detail = detail;
}
void print(){
// detail is never null, so this method won't fail
System.out.println(detail.concat(": ").concat(msg));
}
}
// Client code:
Sub s = new Sub("message"); // Calling Super constructor - detail is never initialized!
s.print(); // throws NullPointerException
从这个例子中,您可以看到您需要某种方式来声明“我想继承这些构造函数”或“我想继承除了这些以外的所有构造函数”,然后您还必须指定默认构造函数继承首选项,以防有人在超类中添加了新的构造函数……或者,如果您想“继承”父类中的构造函数,您可以只要求重复它们,这可以说是更明显的方式。