Java和PHP中的浅拷贝和深拷贝

2021-03-11 18:32

阅读:558

标签:dde   nts   oca   his   object   属性   override   system   protect   

在Java中,需要在被拷贝的类上实现Cloneable接口,重写clone方法即可。浅拷贝只能拷贝类中基本数据类型以及String类型属性,不能拷贝类中的引用类型属性。导致两个类中,同一个引用类型属性指向同一个引用对象。

如果在类的引用类型属性对应的类中,实现Cloneable接口,重写clone方法,就可以实现深拷贝

package fengliang.firstjava;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

class Employee implements Cloneable {
    protected String name;
    protected double salary;
    protected int id = assignId();
    protected static int nextId = 1;
    protected LocalDate hireDay;

    public Employee() {
    }

    public Employee(String var1, double var2, String var4) {
        this.name = var1;
        this.salary = var2;
        this.hireDay = LocalDate.parse(var4);
    }

    public Employee(String var1, double var2) {
        this.name = var1;
        this.salary = var2;
    }

    public String getHireDay() {
        return this.hireDay.format(DateTimeFormatter.ofPattern("y-MM-dd"));
    }

    public String getHireDay(String var1) {
        return this.hireDay.format(DateTimeFormatter.ofPattern(var1));
    }

    public String getName() {
        return this.name;
    }

    public final Employee setName(String var1) {
        this.name = var1;
        return this;
    }

    public final Employee setName() {
        return this;
    }

    public int getId() {
        return this.id;
    }

    protected static int assignId() {
        return nextId++;
    }

    public Employee clone() {
        Employee var1 = null;

        try {
            var1 = (Employee)super.clone();
        } catch (CloneNotSupportedException var3) {
            var3.printStackTrace();
        }

        return var1;
    }
}

//浅拷贝
class ShadowCopy extends Employee> implements Cloneable {

    private T employee;
    private String cardId;

    ShadowCopy() {}
    ShadowCopy(T employee, String cardId) {
        this.employee = employee;
        this.cardId = cardId;
    }

    public T getEmployee() {
        return employee;
    }

    public String getCardId() {
        return cardId;
    }

    public ShadowCopy setEmployee(T employee) {
        this.employee = employee;
        return  this;
    }

    public ShadowCopy setCardId(String cardId) {
        this.cardId = cardId;
        return  this;
    }

    @Override
    public ShadowCopy clone() {
        try {
            return (ShadowCopy) super.clone();
        } catch (CloneNotSupportedException exception) {
            return this;
        }
    }
}

class DeepCopyextends Employee> extends ShadowCopy {

    DeepCopy(T employee, String cardId) {
        super(employee, cardId);
    }

    @Override
    public DeepCopy clone() {
        DeepCopy clone =  (DeepCopy) super.clone();
        clone.setEmployee((T)this.getEmployee().clone());
        return clone;
    }
}

public class CloneTest {
    public static void main(String[] args) {
        Employee employee = new Employee("张三", 12);

        System.out.println("-------------------------------浅拷贝-------------------------------");
        ShadowCopy shadowCopyObject = new ShadowCopy(employee, "00001");
        ShadowCopy clonedShadowCopyObject = shadowCopyObject.clone();
        clonedShadowCopyObject.setCardId("李四"+shadowCopyObject.getCardId()).getEmployee().setName("李四");
        System.out.println("原始shadowCopyObject对象中,Employee对象的name属性值为:"+shadowCopyObject.getEmployee().getName());
        System.out.println("clonedShadowCopyObject对象中,Employee对象的name属性值为:"+clonedShadowCopyObject.getEmployee().getName());
        System.out.println("原始shadowCopyObject对象中,CardId属性值为:"+shadowCopyObject.getCardId());
        System.out.println("clonedShadowCopyObject对象中,CardId属性值为:"+clonedShadowCopyObject.getCardId());

        System.out.println();

        System.out.println("-------------------------------深拷贝-------------------------------");
        Employee deepCopyEmployee = new Employee("王五", 13);
        DeepCopy deepCopyObject = new DeepCopy(deepCopyEmployee, "00002");
        DeepCopy clonedDeepCopyObject = deepCopyObject.clone();
        clonedDeepCopyObject.setCardId("赵六"+deepCopyObject.getCardId()).getEmployee().setName("赵六");
        System.out.println("原始deepCopyObject对象中,Employee对象的name属性值为:"+deepCopyObject.getEmployee().getName());
        System.out.println("clonedDeepCopyObject对象中,Employee对象的name属性值为:"+clonedDeepCopyObject.getEmployee().getName());
        System.out.println("原始deepCopyObject对象中,CardId属性值为:"+deepCopyObject.getCardId());
        System.out.println("clonedDeepCopyObject对象中,CardId属性值为:"+clonedDeepCopyObject.getCardId());
    }
}

 

Java和PHP中的浅拷贝和深拷贝

标签:dde   nts   oca   his   object   属性   override   system   protect   

原文地址:https://www.cnblogs.com/fengliang/p/14121667.html


评论


亲,登录后才可以留言!