码迷,mamicode.com
首页 > 编程语言 > 详细

java中关于对象的复制

时间:2015-06-30 23:12:44      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

java中的对象复制有两种方法,可以简略的称为浅克隆和深克隆

  浅克隆:调用object的clone接口实现克隆,之所以称为浅克隆,是因为在复制的过程中将对象的成员变量中的基本数据类型直接复制,但是对于引用数据类型只是将复制对象成员变量的引用传递过去,并没有对于新对象生成新的成员变量,需要注意的地方,object的clone()方法的访问修饰符是protect的,所以,需要在对象中重写clone方法,将其访问修饰符修改为public方法即可

  深克隆:将当前对象以及对象的成员变量完整的复制,生成一个与之前对象完全相同又毫无关系的对象,需要复制的对象必须实现 Serializable接口,采用输入输出流的方式,获取到当前对象的流文件,复制该流文件,再生成新的对象

以下是所写的事例代码:

package objDeepCopy;

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 DeepCopy {
    
    public static void main(String[] args) throws Exception {
        Teacher teacher=new Teacher();
        teacher.settAge(20);
        teacher.settName("zhangsan");
        Student student1=new Student();
        student1.setsAge(55);
        student1.setsName("xiaojiang");
        student1.setsTeacher(teacher);
        System.out.println("student1"+student1.getsAge()+student1.getsName()+student1.getsTeacher().gettAge()+student1.getsTeacher().gettName());
        Student student2=lowCopy(student1);
        System.out.println("student2"+student2.getsAge()+student2.getsName()+student2.getsTeacher().gettAge()+student2.getsTeacher().gettName());
        //对student1修改,只对复制对象的成员变量进行复制,但是引用的变量只是将引用指向过去
        student1.setsAge(44);
        student1.setsName("xinxiaoming");
        student1.getsTeacher().settName("xinlaoshi");
        System.out.println("------------------");
        System.out.println("student1"+student1.getsAge()+student1.getsName()+student1.getsTeacher().gettAge()+student1.getsTeacher().gettName());
        System.out.println("student2"+student2.getsAge()+student2.getsName()+student2.getsTeacher().gettAge()+student2.getsTeacher().gettName());
    
        
        //深复制通过流,将对象完整的复制过去,并不是引用
//        System.out.println("------------------深复制");
//        
//        Teacher teacher=new Teacher();
//        teacher.settAge(20);
//        teacher.settName("zhangsan");
//        Student student1=new Student();
//        student1.setsAge(55);
//        student1.setsName("xiaojiang");
//        student1.setsTeacher(teacher);
//        System.out.println("student1"+student1.getsAge()+student1.getsName()+student1.getsTeacher().gettAge()+student1.getsTeacher().gettName());
//        Student student2=student1.deepCopy(student1);
//        System.out.println("student2"+student2.getsAge()+student2.getsName()+student2.getsTeacher().gettAge()+student2.getsTeacher().gettName());
//        //对student1修改,只对复制对象的成员变量进行复制,但是引用的变量只是将引用指向过去
//        student1.setsAge(44);
//        student1.setsName("xinxiaoming");
//        student1.getsTeacher().settName("xinlaoshi");
//        System.out.println("------------------");
//        System.out.println("student1"+student1.getsAge()+student1.getsName()+student1.getsTeacher().gettAge()+student1.getsTeacher().gettName());
//        System.out.println("student2"+student2.getsAge()+student2.getsName()+student2.getsTeacher().gettAge()+student2.getsTeacher().gettName());
    
    
    }
    
    
    public static Student lowCopy(Student student) throws Exception{
        Student student2 = (Student) student.clone();
        
        return student2;
    }
    
    
}
class Student implements Serializable,Cloneable{
    /**
     * 
     */
    private static final long serialVersionUID = 5668069747602085306L;
    private int sAge;
    private String sName;
    private    Teacher sTeacher;
    public int getsAge() {
        return sAge;
    }
    public void setsAge(int sAge) {
        this.sAge = sAge;
    }
    public String getsName() {
        return sName;
    }
    public void setsName(String sName) {
        this.sName = sName;
    }
    public Teacher getsTeacher() {
        return sTeacher;
    }
    public void setsTeacher(Teacher sTeacher) {
        this.sTeacher = sTeacher;
    }
    
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    public Student deepCopy(Student student) throws Exception{
        ByteArrayOutputStream boStream=new ByteArrayOutputStream();
        ObjectOutputStream oos= new ObjectOutputStream(boStream);
        oos.writeObject(this);
        ByteArrayInputStream bis=new ByteArrayInputStream(boStream.toByteArray());
        ObjectInputStream ois=new ObjectInputStream(bis);
        return (Student) ois.readObject();
        
    }
    
}

class Teacher implements Serializable{
    private int tAge;
    private String tName;
    public int gettAge() {
        return tAge;
    }
    public void settAge(int tAge) {
        this.tAge = tAge;
    }
    public String gettName() {
        return tName;
    }
    public void settName(String tName) {
        this.tName = tName;
    }
    
    
}

 

java中关于对象的复制

标签:

原文地址:http://www.cnblogs.com/wentjiang/p/4611618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!