Java 浅拷贝和深拷贝的代码示例

串行化实现深拷贝和浅拷贝

package learn.java.DesignPattern.prototype;

import java.io.Serializable;

/** 
* @author  作者 : YUHU YUAN
* @date 创建时间:2016年12月7日 上午11:13:27 
* @version 1.0  
*/

public class Student implements Serializable{

    private static final long serialVersionUID = 1L;

    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }

}
package learn.java.DesignPattern.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/** 
* @author  作者 : YUHU YUAN
* @date 创建时间:2016年12月7日 上午10:27:05 
* @version 1.0  
*/

public class Prototype implements Cloneable, Serializable{

    private static final long serialVersionUID = 1L;

    private int string;

    private Student student;

    public Object clone() throws CloneNotSupportedException{
        Prototype prototype = (Prototype)super.clone();
        return prototype;
    }

    public Object deepClone() throws IOException, ClassNotFoundException{
        /* 写入当前对象的二进制流*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);

        /* 读出二进制流产生的新对象  */
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return ois.readObject();
    }



    public int getString() {
        return string;
    }

    public void setString(int string) {
        this.string = string;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public String toString() {
        return "Prototype [string=" + string + ", student=" + student + "]";
    }

}
package learn.java.DesignPattern.prototype;

import java.io.IOException;

/** 
* @author  作者 : YUHU YUAN
* @date 创建时间:2016年12月7日 上午10:51:41 
* @version 1.0  
*/

public class TestPro {

    public static void main(String[] args) throws CloneNotSupportedException, ClassNotFoundException, IOException {
        // TODO Auto-generated method stub
        Student student = new Student();
        student.setName("yuanyuhu");
        student.setAge(27);
        Prototype prototype = new Prototype();
        prototype.setString(1);
        prototype.setStudent(student);

        Prototype clonePrototype = (Prototype)prototype.clone();

        Prototype deepclonePrototype = (Prototype)prototype.deepClone();


        System.out.println(prototype.toString());

        System.out.println("==========================");

        clonePrototype.setString(2);
        clonePrototype.getStudent().setName("yuanjiajun");
        clonePrototype.getStudent().setAge(25);
        System.out.println(clonePrototype.toString()+"\n"+prototype.toString());
        System.out.println("=================================");
        deepclonePrototype.setString(3);
        deepclonePrototype.getStudent().setName("yuanguangshu");
        deepclonePrototype.getStudent().setAge(53);

        System.out.println(deepclonePrototype.toString()+"\n"+clonePrototype.toString()+"\n"+prototype.toString());


    }

}

通过Object本地方法Clone实现

package learn.java.DesignPattern.prototype;


/** 
* @author  作者 : YUHU YUAN
* @date 创建时间:2016年12月7日 上午11:13:27 
* @version 1.0  
*/

public class Student implements Cloneable{

    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


    public Object clone() throws CloneNotSupportedException{
        Student student = (Student)super.clone();
        return student;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}
package learn.java.DesignPattern.prototype;


/** 
* @author  作者 : YUHU YUAN
* @date 创建时间:2016年12月7日 上午10:27:05 
* @version 1.0  
*/

public class Prototype implements Cloneable{


    private int string;

    private Student student;

    public Object clone() throws CloneNotSupportedException{
        Prototype prototype = (Prototype)super.clone();
        return prototype;
    }


    public Object deepClone() throws CloneNotSupportedException{
        Prototype prototype = (Prototype)super.clone();
        prototype.student = (Student)student.clone();
        return prototype;
    }


    public int getString() {
        return string;
    }

    public void setString(int string) {
        this.string = string;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public String toString() {
        return "Prototype [string=" + string + ", student=" + student + "]";
    }

}


package learn.java.DesignPattern.prototype;

import java.io.IOException;

/** 
* @author  作者 : YUHU YUAN
* @date 创建时间:2016年12月7日 上午10:51:41 
* @version 1.0  
*/

public class TestPro {

    public static void main(String[] args) throws CloneNotSupportedException, ClassNotFoundException, IOException {
        // TODO Auto-generated method stub
        Student student = new Student();
        student.setName("yuanyuhu");
        student.setAge(27);
        Prototype prototype = new Prototype();
        prototype.setString(1);
        prototype.setStudent(student);

        Prototype clonePrototype = (Prototype)prototype.clone();

        Prototype deepclonePrototype = (Prototype)prototype.deepClone();


        System.out.println(prototype.toString());

        System.out.println("==========================");

        clonePrototype.setString(2);
        clonePrototype.getStudent().setName("yuanjiajun");
        clonePrototype.getStudent().setAge(25);
        System.out.println(clonePrototype.toString()+"\n"+prototype.toString());
        System.out.println("=================================");
        deepclonePrototype.setString(3);
        deepclonePrototype.getStudent().setName("yuanguangshu");
        deepclonePrototype.getStudent().setAge(53);

        System.out.println(deepclonePrototype.toString()+"\n"+clonePrototype.toString()+"\n"+prototype.toString());


    }

}

至于说他们之间的区别,应该是特定的情况下用串行化实现,不过之前的时候没有用到过拷贝的情况,这个问题是在学习设计模式的原型模式提出来的。以后有新的想法再补充把。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值