对象的克隆

Java基础

浏览数:99

2019-8-21

AD:资源代下载服务

对象的克隆

对象地址的引用:

public class Person {
    
    int id;
    
    String name;

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
        
    }
    
    @Override
    public String toString() {
        return "编号:"+ this.id+" 姓名:"+ this.name;
    }
    
}


public class Demo {
    
    public static void main(String[] args) throws Exception {
        
        Person p1 = new Person(123,"hcx");
        
        Person p2 = p1;
        System.out.println("p1:"+p1);//p1:编号:123 姓名:hcx
        System.out.println("p2:"+ p2);//p2:编号:123 姓名:hcx

    }

}

以上属于对象地址的引用:

对象地址的引用.png

一、对象的浅克隆

Person:

class Address{
    
    String city;
        
    public Address(String city){
        this.city = city;
    }
    
}



public class Person implements Cloneable{
    
    int id;
    
    String name;
    
    Address address;

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
        
    }
    
    public Person(int id, String name, Address address) {
        this.id = id;
        this.name = name;
        this.address = address;
        System.out.println("=======构造方法调用了===");
    }




    @Override
    public String toString() {
        return "编号:"+ this.id+" 姓名:"+ this.name+" 地址:"+ address.city;
    }
    
    
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    

}

Demo:

public class Demo {
    
    public static void main(String[] args) throws Exception {
        
        Address address = new Address("深圳");
        Person p1 = new Person(123,"hcx",address);
        Person p2 = (Person) p1.clone(); //clone() 克隆了一个对象。
        
        p2.name = "gia";
        p2.address.city ="广州";
        System.out.println("p1:"+p1);
        System.out.println("p2:"+ p2);
  
        System.out.println("p1:"+p1);//p1:编号:123 姓名:hcx 地址:广州
        System.out.println("p2:"+ p2);//p2:编号:123 姓名:gia 地址:广州

    }

}

对象的浅克隆.png

对象浅克隆要注意的细节:

  1. 如果一个对象需要调用clone的方法克隆,那么该对象所属的类必须要实现Cloneable接口。
  2. Cloneable接口是一个标识接口而已,没有任何方法。
  3. 对象的浅克隆就是克隆一个对象的时候,如果被克隆的对象中维护了另外一个类的对象,这时候只是克隆另外一个对象的地址,而没有把另外一个对象也克隆一份。
  4. 对象的浅克隆不会调用到构造方法的。

二、对象的深克隆

对象的深克隆:利用对象的输入输出流把对象先写到文件上,然后再读取对象的信息。这个过程就称作为对象的深克隆。

Person:

import java.io.Serializable;

class Address implements Serializable{
    
    String city;
        
    public Address(String city){
        this.city = city;
    }
    
}



public class Person implements Cloneable,Serializable {
    
    int id;
    
    String name;
    
    Address address;

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
        
    }
    
    
    
    
    public Person(int id, String name, Address address) {
        this.id = id;
        this.name = name;
        this.address = address;
        System.out.println("=======构造方法调用了===");
    }




    @Override
    public String toString() {
        return "编号:"+ this.id+" 姓名:"+ this.name+" 地址:"+ address.city;
    }
    
    
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    

}

Demo2:

public class Demo2 {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Address address = new Address("深圳");
        Person p1 = new Person(123,"hcx",address);
        writeObj(p1);
        Person p2  =readObj();
        
        p2.address.city = "广州";
        System.out.println("p1:"+ p1);//p1:编号:123 姓名:hcx 地址:深圳
        System.out.println("p2:"+ p2);//p1:编号:123 姓名:hcx 地址:广州
        
        
    }
    
    
    //再从文件中读取对象的信息
    public static Person readObj() throws ClassNotFoundException, IOException{
        FileInputStream fileInputStream = new FileInputStream("F:\\obj.txt");
        //创建对象的输入流对象
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        return (Person) objectInputStream.readObject();
    }
    
    
    //先要把对象写到文件上。
    public static void writeObj(Person p) throws IOException{
        //建立一个文件 的输出流对象
        FileOutputStream fileOutputStream  = new FileOutputStream("F:\\obj.txt");
        //建立对象的输出流
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        //把对象写出
        objectOutputStream.writeObject(p);
        //关闭资源
        objectOutputStream.close();
        
    }

}

作者:JS_HCX