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

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

时间:2020-12-17 12:02:07      阅读:2      评论:0      收藏:0      [点我收藏+]

标签: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 <T 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<T> setEmployee(T employee) {
        this.employee = employee;
        return  this;
    }

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

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

class DeepCopy<T extends Employee> extends ShadowCopy<T> {

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

    @Override
    public DeepCopy<T> clone() {
        DeepCopy<T> clone =  (DeepCopy<T>) 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<Employee> shadowCopyObject = new ShadowCopy<Employee>(employee, "00001");
        ShadowCopy<Employee> 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<Employee> deepCopyObject = new DeepCopy<>(deepCopyEmployee, "00002");
        DeepCopy<Employee> 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

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