class TestClone implements Cloneable {
  int a;
  double b;
  TestClone cloneTest() {
     try {
       return (TestClone) super.clone();
     }
     catch(CloneNotSupportedException e) {
        System.out.println("Cloning not allowed.");
       return this;
  }
}
}
class CloneDemo {
  public static void main(String args[]) {
     TestClone x1=new TestClone();
     TestClone x2;
     x1.a=10;
     x1.b=20.67;
     x2=x1.cloneTest();
     System.out.println("x1: "+x1.a+" "+x1.b);
     System.out.println("x2: "+x2.a+" "+x2.b);
  }
}这里的super.clone()的意义,(TestClone)对上面所作的改变

解决方案 »

  1.   

    调用Object类的clone(),返回的是一个Object对象。所以需要 (TestClone) 强制类型转换。
      

  2.   

    其实你做的就是自己写一个clone方法,在里面调用父类的就是object的clone方法,它的返回类型是object,你得把这个转型为你自己的类TestClone 
      

  3.   

    调用Object基类的clone()方法来实现对对象的拷贝。