经过一番搜索,我没有找到有关复制构造函数和继承的任何好答案.
我有两个班:用户和学员.受训者从User继承,并且两个String参数被添加到受训者.
现在,我设法制作了User的副本构造函数,但对Trainee的副本构造函数不满意.
用户复制构造函数的代码如下:
public User (User clone) {
this(clone.getId(),
clone.getCivilite(),
clone.getNom(),
clone.getPrenom(),
clone.getEmail(),
clone.getLogin(),
clone.getTel(),
clone.getPortable(),
clone.getInscription(),
clone.getPw()
);
}
我尝试在我的Trainee复制构造函数中使用super:
public Trainee (Trainee clone) {
super (clone);
this (clone.getOsia(), clone.getDateNaiss());
}
但这没有用,我被迫编写完整版本的复制构造函数的代码:
public Trainee (Trainee clone) {
this(clone.getId(),
clone.getCivilite(),
clone.getNom(),
clone.getPrenom(),
clone.getEmail(),
clone.getLogin(),
clone.getTel(),
clone.getPortable(),
clone.getInscription(),
clone.getPw(),
clone.getOsia(),
clone.getDateNaiss()
);
}
由于主体结构的原因,我必须像这样强制转换新实例:
User train = new Trainee();
User train2 = new Trainee((Trainee) train);
所以我的问题是:是否有更清洁的方法来做到这一点?我不能使用超级吗?
预先感谢您的答复和协助.