[java] java对象的克隆

一、对象实例化后赋值给一个新对象

package com.wa.java.book.kernel.oo;

/**
 * Created by Administrator on 2015/2/9.
 */
public class Dog {

    private Integer id;
    private String name ;
    private String color ;
    private Double age ;

    public Dog() {
    }

    public Dog(Integer id, String name, String color, Double age) {
        this.id = id;
        this.name = name;
        this.color = color;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Double getAge() {
        return age;
    }

    public void setAge(Double age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", age=" + age +
                '}';
    }



    public static void main(String[] args){

        Dog dog = new Dog(1,"小黑","黑白",7.2);

        System.out.println(dog);//Dog{id=1, name='小黑', color='黑白', age=7.2}
//把dog对象实例化后 赋值给dog1
        Dog dog1 = dog;
        dog1.setName("黑子");
        dog1.setAge(3.2);
        dog1.setColor("黄色");
        dog1.setId(12);
        System.out.println(dog1);//Dog{id=12, name='黑子', color='黄色', age=3.2}
        System.out.println(dog);//Dog{id=12, name='黑子', color='黄色', age=3.2}

        System.out.println(dog==dog1);//true


    }

}

 1.java里的clone分为: 
A:浅复制(浅克隆): 浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。 
b:深复制(深克隆):深复制把要复制的对象所引用的对象都复制了一遍。 
Java中对象的克隆,为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。必须要遵循下面三点 
1.在派生类中覆盖基类的clone()方法,并声明为public【Object类中的clone()方法为protected的】。 
2.在派生类的clone()方法中,调用super.clone()。 
3.在派生类中实现Cloneable接口。

浅克隆:

package com.wa.java.book.kernel.oo;

/**
 * Created by Administrator on 2015/2/10.
 */
public class Tree {
    private String name ;
    private String country;

    public Tree() {
    }

    public Tree(String country, String name) {
        this.country = country;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "Tree{" +
                "name='" + name + '\'' +
                ", country='" + country + '\'' +
                '}';
    }


}

 class Flower implements  Cloneable{
    private String name ;
    private String color;
   private Tree tree;
    public Flower() {
    }

     public Flower(Tree tree, String color, String name) {
         this.tree = tree;
         this.color = color;
         this.name = name;
     }

     public Tree getTree() {
         return tree;
     }

     public void setTree(Tree tree) {
         this.tree = tree;
     }

     public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
     //重写clone方法
     @Override
     protected Object clone() throws CloneNotSupportedException {
         return super.clone();
     }

     @Override
     public String toString() {
         return "Flower{" +
                 "name='" + name + '\'' +
                 ", color='" + color + '\'' +
                 ", tree=" + tree +
                 '}';
     }
 }

测试:

 /**
     * 浅克隆 只克隆他考虑的对象但不克隆引用他的对象
     * @throws CloneNotSupportedException
     */
    @org.junit.Test
    public void test1() throws CloneNotSupportedException {

        Tree tree= new Tree();
        tree.setName("水杉");
        tree.setCountry("中国");

        Flower flower = new Flower();
        flower.setName("牡丹");
        flower.setColor("粉红色");
        flower.setTree(tree);
System.out.println(flower);//Flower{name='牡丹', color='粉红色', tree=Tree{name='水杉', country='中国'}}

       //克隆一个对象
        Flower flower1 = (Flower) flower.clone();

        flower1.setName("郁金香");
        flower1.setColor("白色");

        System.out.println(flower1.getTree().getCountry());//中国
        System.out.println(flower1.getTree().getName());//水杉
        flower1.getTree().setCountry("美国");
        flower1.getTree().setName("紫衫");
/**
 * 可以看到它仅仅克隆了Flower对象
 */
System.out.println(flower1);//Flower{name='郁金香', color='白色', tree=Tree{name='紫衫', country='美国'}}
System.out.println(flower);//Flower{name='牡丹', color='粉红色', tree=Tree{name='紫衫', country='美国'}}

    }

深度克隆:

package com.wa.java.book.kernel.oo;

/**
 * Created by Administrator on 2015/2/10.
 */
public class Tree implements Cloneable{
    private String name ;
    private String country;

    public Tree() {
    }

    public Tree(String country, String name) {
        this.country = country;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Tree{" +
                "name='" + name + '\'' +
                ", country='" + country + '\'' +
                '}';
    }


}

 class Flower implements  Cloneable{
    private String name ;
    private String color;
   private Tree tree;
    public Flower() {
    }

     public Flower(Tree tree, String color, String name) {
         this.tree = tree;
         this.color = color;
         this.name = name;
     }

     public Tree getTree() {
         return tree;
     }

     public void setTree(Tree tree) {
         this.tree = tree;
     }

     public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

     //重写clone方法
     @Override
     protected Object clone() throws CloneNotSupportedException {
         Flower flower = (Flower) super.clone();
         flower.setTree((Tree) flower.getTree().clone());
         return flower;
     }

     @Override
     public String toString() {
         return "Flower{" +
                 "name='" + name + '\'' +
                 ", color='" + color + '\'' +
                 ", tree=" + tree +
                 '}';
     }
 }

测试:

 @org.junit.Test
    public void test2() throws CloneNotSupportedException {

        Tree tree= new Tree();
        tree.setName("水杉");
        tree.setCountry("中国");

        Flower flower = new Flower();
        flower.setName("牡丹");
        flower.setColor("粉红色");
        flower.setTree(tree);
        System.out.println(flower);//Flower{name='牡丹', color='粉红色', tree=Tree{name='水杉', country='中国'}}

        //克隆一个对象
        Flower flower1 = (Flower) flower.clone();

        flower1.setName("郁金香");
        flower1.setColor("白色");

        System.out.println(flower1.getTree().getCountry());//中国
        System.out.println(flower1.getTree().getName());//水杉
        flower1.getTree().setCountry("美国");
        flower1.getTree().setName("紫衫");
/**
 * 可以看到它仅仅克隆了Flower对象
 */
        System.out.println(flower1);//Flower{name='郁金香', color='白色', tree=Tree{name='紫衫', country='美国'}}
        System.out.println(flower);//Flower{name='牡丹', color='粉红色', tree=Tree{name='水杉', country='中国'}}
    }

还有序列化的克隆

 

 

 

参考资料:

http://blog.csdn.net/jdluojing/article/details/6963112

posted @ 2015-02-10 20:27  snow__wolf  阅读(163)  评论(0)    收藏  举报