編輯:我遇到這個問題,當我需要克隆一個對象,我已經搜索,但沒有問題解決我的問題。Java:類型轉換克隆之前?
當我在Java中學習原型模式時,我跟隨了一些教程,並對(Bike)super.clone()
的類型轉換感到困惑。
首先完成哪項工作? (Bike)super
或super.clone()
?爲什麼需要(Bike)
?
public interface Prototype extends Cloneable
{
public Prototype clone() throws CloneNotSupportedException;
}
public class Bike implements Prototype
{
// Constructor and other methods...
// My question is here
public Prototype clone() throws CloneNotSupportedException {
return (Bike)super.clone();
}
}
[Java cast order]的可能重複(http://stackoverflow.com/questions/5762270/java-鑄造順序) – Tom