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

来源:互联网 发布:淘宝 手机店 编辑:程序博客网 时间:2024/05/21 18:32

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

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
原创粉丝点击