經過一番搜索後,我沒有找到任何有關複製構造函數和繼承問題的良好答案。 我有兩個類:用戶和受訓者。學員繼承自用戶,並將兩個字符串參數添加到學員。 現在我設法創建了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