对象的克隆

对象的浅克隆

/*
 对象的克隆

    对象的浅克隆:

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

    对象的深克隆:

 */
public class Demo1 {


    public static void main(String[] args) throws Exception {
        Address address = new Address("广州");
        Person p1 = new Person(110,"狗娃",address);
        Person p2 = (Person) p1.clone(); //clone() 克隆了一个对象。

        p2.name = "狗剩";
        p2.address.city ="长沙";
        System.out.println("p1:"+p1);
        System.out.println("p2:"+ p2);



    }

}

对象的深克隆


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

    ObjectInputStream
    ObjectOutputStream 

 */
public class Demo2 {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Address address = new Address("广州");
        Person p1 = new Person(110,"狗娃",address);
        writeObj(p1);
        Person p2  =readObj();

        p2.address.city = "长沙";
        System.out.println("p1:"+ p1);
        System.out.println("p2:"+ p2);


    }


    //再从文件中读取对象的信息
    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();

    }
}

Person类

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();
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值