Clone使用

一)克隆接口的说明:

一个对象要想实现克隆,实现克隆接口:clone方法是默认是Object的方法

1)这个接口是一个标记性的接口(空接口),内部都没有方法和属性,实现这个接口表示这个对象都可以进行克隆,调用Object对象的Object.clone()方法,如果没有实现Cloneable的类对象调用clone()就会抛出CloneNotSupportedException异常

2)想要一个类具备拷贝实例的功能,除了要实现Cloneable接口,还必须要重写Object类的clone()方法

3)我们可以看到Object类的clone()方法是被protected修饰的,我们需要在重写的clone()方法通过super关键字调用Object的clone()方法,

4)Cloneable接口发挥的是标记功能,自定义类型需要用户自己去标记哪些类是可以被clone的,这个标记就是去实现Cloneable接口,实现该接口的类就表示该类创建的对象可以被克隆

5)想要克隆自定义类型,就要实现克隆接口public interface Cloneable(){};空接口也叫作标记接口

二)浅拷贝:

浅拷贝:在拷贝一个对象的时候,只是对对象的基本数据类型的成员变量进行拷贝,但是对引用类型的成员变量只进行引用的传递,针对引用数据类型来说并没有创建一个新的对象,当对引用类型的修改会影响到要拷贝的对象,简而言之,浅拷贝只是复制所拷贝的地址,而不复制他的所引用的对象


@Data
class Student implements Cloneable{
    public String username;
    public String password;
    public Student(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Money money=(Money)super.clone();
    }
}
public class HelloWorld{
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1=new Student("李佳伟","12503487");
        Student student2= (Student) student1.clone();
        System.out.println(student1==student2);
//false,两个引用分别指向的对象的地址值不相同
    }
}

false
Student{username='李佳伟', password='12503487'}
Student{username='李佳伟', password='12503487'}
 

上面画的图就是克隆出来了一个新的对象和新的引用,但是由于这个对象的组成全部是一个基本数据类型,所以只对对象中的基本数据类型复制了一份

class Money{
    public double m=100.5;
        }
class Student implements Cloneable
{
    String name;
//实现这个接口要重写克隆方法
Money money=new Money();
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class Tomact {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1=new Student();
        Student student2= (Student) student1.clone();
        System.out.println(student1.money.m);
        System.out.println(student2.money.m);
        student2.money.m=199;
        System.out.println(student1.money.m);
        System.out.println(student2.money.m);
//都变成了199
    }
}

1)此时单纯的修改student1的username此时是不会修改student2的username的

2) 我们观察上面的代码,我们将Money类的实例引用作为了Student类成员变量的一个字段,此时克隆操作将这个student1成员变量的Money引用地址拷贝到student2对象里面,这里面的拷贝就是浅拷贝了,只是将student1的对象的money的引用进行拷贝了一份,此时student1和student2对象中的两个相同的money引用指向的是同一个对象,因为只是拷贝了一份地址

3)由此看出,只是拷贝了money对象的引用的地址,但是这两个引用仍然指向的是同一个对象,此时原来的对象和新被克隆出来的对象的成员变量money指向地址的都是 0x100

三)深拷贝以及实现: 

class Money implements Cloneable{
    public double m=100.5;
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return  super.clone();
    }
}
class Student implements Cloneable
{
    String name;
    //实现这个接口要重写克隆方法
    Money money=new Money();
    @Override
    protected Object clone() throws CloneNotSupportedException {

        //return super.clone();正常情况下,Person的克隆是在这里面执行
        //Student s=(Student) super.clone();
        //return s;
        //1克隆Person
        Student s= (Student)super.clone();
        //2克隆当前的Money对象
        s.money=(Money)this.money.clone();
        return s;
        // 由于下面要进行Money的克隆
    }
}

public class Tomact {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1=new Student();
        Student student2= (Student) student1.clone();
        System.out.println(student1.money.m);
        System.out.println(student2.money.m);
        student2.money.m=199;
        System.out.println(student1.money.m);
        System.out.println(student2.money.m);
        //这里面只是克隆了一个person,我们要想实现深拷贝
        //就要把Student对象中的money这个对象也克隆一份
    }
}

Java中的clone方法是一个浅拷贝,引用类型仍然在传递引用,可以通过继续调用clone方法,对该对象的引用类型变量在实现一次clone的方法来实现深拷贝

1)让我们的两个类都实现cloneable接口

2)在我们的Student类里面不光克隆Student对象里面的属性,还进行克隆Student类中的引用类型所指向的对象也进行克隆一份

class Money implements Cloneable{
    public int money;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Student implements Cloneable{
    public String username;
    public String password;
    public Student(String username, String password) {
        this.username = username;
        this.password = password;
    }
    Money money=new Money();
    @Override
    protected Object clone() throws CloneNotSupportedException {
       Student student=(Student)super.clone();
       student.money=(Money)money.clone();
       return student;
    }
    @Override
    public String toString() {
        return "Student{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
public class HelloWorld{
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1=new Student("李佳伟","12503487");
        Student student2= (Student) student1.clone();
        student1.money.money=999;
        System.out.println(student1.money.money);//999
        System.out.println(student2.money.money);//0
    }
}

拷贝简单类型是深拷贝,拷贝引用类型是浅拷贝,我们要想实现深拷贝,就要拷贝一下这个对象,只是拷贝地址值,是不能实现真正意义上的深拷贝的

1)咱们的数组的四种拷贝方法都是浅拷贝,因为我们大部分情况下是拷贝地址的,而现在是拷贝数据

2)深拷贝:如果我们拷贝完成之后,我们通过一个引用去访问去修改拷贝完成之后的值,是不会修改原来的值

3)浅拷贝:相反拷贝通过引用来进行修改,原来的值发生变化,就叫做浅拷贝

如果问数组的拷贝是什么拷贝?先问问到底是再拷贝什么???

static关键字修饰的成员是属于类的,而abstract一般是用来修饰普通方法目的是为了让子类继承后重写该方法,而static修饰的方法是不存在继承重写的

创建对象的方式:

  1. new关键字
  2. 反射
  3. Clone方法
  4. 反序列化使用ObjectMapper

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值