[英]What should be the return type of clone()?
例如,我有類Human,我想覆蓋clone()函數。
clone(),Object還是Human的返回類型應該是什么? 我知道返回類型在重寫過程中沒有任何作用,因為它不在函數的簽名中。
例如,在課堂上我應該擁有
public Object clone() throws CloneNotSupportedException {
Human h = (Human)super.clone();
h.age = age;
h.name = name;
return h;
}
然后在主要的
public static void main() throws CloneNotSupportedException {
Human h = new Human("Slavco", 49);
Human z = (Human)h.clone();
}
要么
public Human clone() throws CloneNotSupportedException {
Human h = (Human)super.clone();
h.age = age;
h.name = name;
return h;
}
在主要的
public static void main() throws CloneNotSupportedException {
Human h = new Human("Slavco", 49);
Human z = h.clone();
}
回歸Human
會讓你的生活變得更輕松(可能會為你節省很多時間),並且沒有任何不利因素。
我肯定會推薦這種方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.