经过一番搜索后,我没有找到任何有关复制构造函数和继承问题的良好答案。 我有两个类:用户和受训者。学员继承自用户,并将两个字符串参数添加到学员。 现在我设法创建了User的复制构造函数,但我对Trainee的复制构造函数不满意。 用户拷贝构造函数的代码是这样的:Java复制构造函数和继承
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()
);
}
我想在我见习的拷贝构造函数使用超:
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);
所以我的问题是:是否有更干净的方法来做到这一点?我不能使用超级?
非常感谢您的回答和帮助。
当你使用'超(克隆)什么没有工作' – Xymostech