java对象复制

一,a和b都指向同一个对象,改变其中一个另一个也会改变

package com.ciaos;

class Human{
    public Human(String string, int i) {
        // TODO Auto-generated constructor stub
        name = string;
        age = i;
    }
    String name;
    int age;
}

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
/*        int num[][] = {{1,2,3,4},{5,6,7,8},{2,3,4,5},{4,5,6,7}};
        for(int x[]:num){
            for(int y:x){
                System.out.print(y);
            }
        }*/
        Human a = new Human("ciaos",26);
        System.out.println("a:"+a.name+" "+a.age);//a:ciaos 26
        
        Human b = a;
        b.name = "stone";
        System.out.println("a:"+a.name+" "+a.age);//a:stone 26
        System.out.println("b:"+b.name+" "+b.age);//b:stone 26
    }
}

二,继承Cloneable接口,实现clone方法,实现浅拷贝

package com.ciaos;

class Human implements Cloneable{
    public Human(String string, int i) {
        // TODO Auto-generated constructor stub
        name = string;
        age = i;
    }
    String name;
    int age;
    
    public Object clone(){
        Human h = null;
        try {
            h = (Human)super.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return h;
    }
}

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Human a = new Human("ciaos",26);
        System.out.println("a:"+a.name+" "+a.age);//a:ciaos 26
        
        Human b = (Human)a.clone();
        b.name = "stone";
        System.out.println("a:"+a.name+" "+a.age);//a:ciaos 26
        System.out.println("b:"+b.name+" "+b.age);//b:stone 26
    }
}

三,如果对象中有变量指向别的对象,成员变量指向的对象仍然是同样的

package com.ciaos;

class Addr{
    public Addr(String country2, String city2) {
        // TODO Auto-generated constructor stub
        country = country2;
        city = city2;
    }
    String country;
    String city;
}

class Human implements Cloneable{
    public Human(String string, int i, String country, String city) {
        // TODO Auto-generated constructor stub
        name = string;
        age = i;
        addr = new Addr(country,city);
    }
    String name;
    int age;
    Addr addr;
    
    public Object clone(){
        Human h = null;
        try {
            h = (Human)super.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return h;
    }
}

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Human a = new Human("ciaos",26,"china","shenzhen");
        System.out.println("a:"+a.name+" "+a.age+" "+a.addr.country+" "+a.addr.city);//a:ciaos 26 china shenzhen
        
        Human b = (Human)a.clone();
        b.name = "stone";
        b.addr.city = "shanghai";
        System.out.println("a:"+a.name+" "+a.age+" "+a.addr.country+" "+a.addr.city);//a:ciaos 26 china shanghai
        System.out.println("b:"+b.name+" "+b.age+" "+b.addr.country+" "+b.addr.city);//b:stone 26 china shanghai
    }
}

四,需要对成员变量指向对象的类继承拷贝接口,修改Human类复制方法

package com.ciaos;

class Addr implements Cloneable{
    public Addr(String country2, String city2) {
        // TODO Auto-generated constructor stub
        country = country2;
        city = city2;
    }
    String country;
    String city;
    public Object clone(){
        Addr a = null;
        try {
            a = (Addr)super.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return a;
    }
}

class Human implements Cloneable{
    public Human(String string, int i, String country, String city) {
        // TODO Auto-generated constructor stub
        name = string;
        age = i;
        addr = new Addr(country,city);
    }
    String name;
    int age;
    Addr addr;
    
    public Object clone(){
        Human h = null;
        try {
            h = (Human)super.clone();
            h.addr = (Addr)this.addr.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return h;
    }
}

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Human a = new Human("ciaos",26,"china","shenzhen");
        System.out.println("a:"+a.name+" "+a.age+" "+a.addr.country+" "+a.addr.city);//a:ciaos 26 china shenzhen
        
        Human b = (Human)a.clone();
        b.name = "stone";
        b.addr.city = "shanghai";
        System.out.println("a:"+a.name+" "+a.age+" "+a.addr.country+" "+a.addr.city);//a:ciaos 26 china shenzhen
        System.out.println("b:"+b.name+" "+b.age+" "+b.addr.country+" "+b.addr.city);//b:stone 26 china shanghai
    }
}

 

posted @ 2014-01-01 09:46  ciaos  阅读(289)  评论(0编辑  收藏  举报