关闭

java深克隆浅克隆

107人阅读 评论(0) 收藏 举报
分类:
浅克隆:只复制当前类的简单属性
深克隆:复制当前类中的简单属性及复杂类型属性。
可能说的不是太清楚,运行如下代码就都清楚啦!

Student.java
package com.hitachi.clone;

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

public class Student implements Cloneable,Serializable {

    /**
    * @Fields serialVersionUID : TODO 变量名称
    */
    private static final long serialVersionUID = 5474483935180730022L;

    private String name;

    private Integer age;

    private Professor professor;


    public String getName() {
        return name ;
    }


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


    public Integer getAge() {
        return age ;
    }


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


    public Professor getProfessor() {
        return professor ;
    }


    public void setProfessor(Professor professor ) {
        this.professor = professor ;
    }

    /**
     *
     * @Description:浅克隆
     * @return: 返回结果描述
     * @return Object: 返回值类型
     * @throws
     */
    public Object shallowClone() {  
        Object o= null;  
        try {  
            o= super.clone();//Object 中的clone()识别出你要复制的是哪一个对象。  
        } catch(CloneNotSupportedException e ) {  
            System. out.println(e .toString());  
        }  
        return o ;  
    }
   
    /**
     *
     * @Description:深克隆
     * @return
     * @throws IOException
     * @throws ClassNotFoundException: 返回结果描述
     * @return Object: 返回值类型
     * @throws
     */
    public Object deepClone() throws IOException, ClassNotFoundException{
        ByteArrayOutputStream out= new ByteArrayOutputStream();
        ObjectOutputStream oos= new ObjectOutputStream(out );
        oos.writeObject( this);
       
        ByteArrayInputStream input= new ByteArrayInputStream(out.toByteArray());
        ObjectInputStream ois= new ObjectInputStream(input );
        return ois .readObject();
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", professor=" + professor + "]" ;
    }   
   
}
Professor.java
package com.hitachi.clone;

import java.io.Serializable;

public class Professor implements Serializable {
   
    /**
    * @Fields serialVersionUID : TODO 变量名称
    */
    private static final long serialVersionUID = -3321860827572727878L;

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

    public Integer getAge() {
        return age ;
    }

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

    @Override
    public String toString() {
        return "Professor [name=" + name + ", age=" + age + "]" ;
    }
   
}
Test.java
package com.hitachi.clone;

import java.io.IOException;

public class Test {
   
    public static void main(String[] args) {
        Student stu= new Student();
        stu.setAge(23);
        stu.setName( "小鸟");
        Professor pro= new Professor();
        pro.setAge(42);
        pro.setName( "大鸟");
        stu.setProfessor( pro);
        Student stu2=(Student) stu.shallowClone();
        stu2.setAge(24);
        stu2.setName( "小小");
        stu2.getProfessor().setAge(43);
        System. out.println(stu );
        System. out.println(stu2 );
       
        Student stu1= new Student();
        stu1.setAge(23);
        stu1.setName( "小鸟");
        Professor pro1= new Professor();
        pro1.setAge(42);
        pro1.setName( "大鸟");
        stu1.setProfessor( pro1);
        try {
            Student stu3 = (Student) stu1.deepClone();
            stu3.setAge(24);
            stu3.setName( "小小");
            stu3.getProfessor().setAge(44);
            System. out.println(stu1 );
            System. out.println(stu3 );
        } catch (ClassNotFoundException e ) {
            e.printStackTrace();
        } catch (IOException e ) {
            e.printStackTrace();
        }
       
    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5862次
    • 积分:203
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:6篇
    • 译文:0篇
    • 评论:0条
    文章分类