本帖最后由 FcBayernMunchen 于 2010-07-22 12:22:24 编辑

解决方案 »

  1.   

    public class A implements Cloneable {   
        public String name;   
      
        public Object clone() {   
            A o = null;   
            try {   
                o = (A) super.clone();   
            } catch (CloneNotSupportedException e) {   
                e.printStackTrace();   
            }   
            return o;   
        }   
      
    }  
      

  2.   

    Object中的clone方法是protected。没有override的话,怎么调用?@Override
    public Object clone(){
        // 
    }
    默认的浅拷贝实现只能满足某些情况下的需求。
      

  3.   

       楼上 两位的我都知道,我的意思不是说怎么用 clone() ,而是说我单独写一个方法,里面 super.clone() 也可以,不一定非要覆盖 clone() 才能实现克隆,如 例子中的 ss()
      

  4.   

    实现 Cloneable() 接口
    然后覆盖
      

  5.   

    public class Test implements Cloneable{
        String name;
        public Test(String name){this.name=name;}
        public String toString(){ return name;}
        public Test copy(){
    Test t = null;
    try {
        t = (Test)super.clone();
        t.name = this.name;
    }catch (Exception e){
        System.out.println(e);
    }
    return t;
        }
        
        public static void main(String[] args) {
    Test t = new Test("test");
    System.out.println(t);
    System.out.println(t.copy());
        }
    }
      

  6.   


     是的,你实现克隆了,但你的方法不是clone()所以你根本不用实现 Cloneable 接口,如果要实现Cloneable就要遵守Cloneable约定。提供public的clone()方法并保证工作良好。(比如深克隆)
      

  7.   

      不行,不实现 Cloneable 接口的话,在 ss() 中调用 super.clone() 会报异常的
      

  8.   

    http://topic.csdn.net/u/20100613/09/44ec023f-4560-43ce-a27b-2b34ec198163.html?21761