設計模式之Prototype

esuchj發表於2005-04-21
public abstract class AbstractSpoon implements Cloneable
{
  String spoonName;

  public void setSpoonName(String spoonName) {this.spoonName = spoonName;}
  public String getSpoonName() {return this.spoonName;}

  public Object clone()
  {
    Object object = null;
    try {
      object = super.clone();
    } catch (CloneNotSupportedException exception) {
      System.err.println("AbstractSpoon is not Cloneable");
    }
    return object;
  }
}


有個具體實現(ConcretePrototype):

public class SoupSpoon extends AbstractSpoon
{
  public SoupSpoon()
  {
    setSpoonName("Soup Spoon");
  }
}

呼叫Prototype模式很簡單:

AbstractSpoon spoon = new SoupSpoon();
AbstractSpoon spoon2 = spoon.clone();


我在Jbuilder2005下試驗了這些程式碼,編譯器居然報錯(指向spoon.clone();),提示incompatible types;Found:java.lang.Object,required:spoon.AbstractSpoon
不解ing。。。

// 在此輸入java程式碼
<p class="indent">

相關文章