• Java浅拷贝和深拷贝概念以及实现


    Java浅拷贝和深拷贝概念以及实现

     在Java语言中,因为有引用类型的变量,所以也会有浅拷贝与深拷贝的区别

     但是对于Java中的基础类型byte boolean char short int long float double以及它们的封装类型都实现了深拷贝

    浅拷贝代码示例

    package priv.jack.jdk.demo;
    
    /**
     * 浅拷贝
     * @author Jack
     *
     */
    public class ShallowCopy {
    
        public static void main(String[] args) throws Exception {
            Wallet wallet = new Wallet("LV", 10000);
            Person p1 = new Person("curry", 28, wallet);
            Person p2 = (Person) p1.clone();
            p2.getWallet().setName("保罗");
            p2.getWallet().setMoney(60);
            p2.setName("stu");
            p2.setAge(30);
            System.out.println(p1.toString());
            System.out.println(p2.toString());
        }
    
    }
    
    class Wallet implements Cloneable {
        private String name;
        private long money;
    
        public Wallet(String name, int money) {
            this.name = name;
            this.money = money;
        }
    
        @Override
        public String toString() {
            return "Wallet [name=" + name + ", money=" + money + "]";
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public long getMoney() {
            return money;
        }
    
        public void setMoney(long money) {
            this.money = money;
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    
    }
    
    class Person implements Cloneable {
        private String name;
        private int age;
        private Wallet wallet;
    
        public Person(String name, int age, Wallet wallet) {
            this.name = name;
            this.age = age;
            this.wallet = wallet;
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", p=" + wallet + "]";
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Wallet getWallet() {
            return wallet;
        }
    
        public void setWallet(Wallet wallet) {
            this.wallet = wallet;
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    
    }

    运行结果如下

    Person [name=curry, age=28, p=Wallet [name=保罗, money=60]]
    Person [name=stu, age=30, p=Wallet [name=保罗, money=60]]

    钱包的名称和金额都变了,说明两个人公用一个钱包,这样不好吧

    深拷贝实例一

    package priv.jack.jdk.demo;
    
    /**
     * 深拷贝
     * @author Jack
     *
     */
    public class DeepCopy1 {
    
        public static void main(String[] args) throws Exception {
            Wallet1 Wallet1 = new Wallet1("LV", 10000);
            Person1 p1 = new Person1("curry", 28, Wallet1);
            Person1 p2 = (Person1) p1.clone();
            p2.getWallet1().setName("保罗");
            p2.getWallet1().setMoney(60);
            p2.setName("stu");
            p2.setAge(30);
            System.out.println(p1.toString());
            System.out.println(p2.toString());
        }
    
    }
    
    class Wallet1 implements Cloneable {
        private String name;
        private long money;
    
        public Wallet1(String name, int money) {
            this.name = name;
            this.money = money;
        }
    
        @Override
        public String toString() {
            return "Wallet1 [name=" + name + ", money=" + money + "]";
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public long getMoney() {
            return money;
        }
    
        public void setMoney(long money) {
            this.money = money;
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    
    }
    
    class Person1 implements Cloneable {
        private String name;
        private int age;
        private Wallet1 wallet;
    
        public Person1(String name, int age, Wallet1 Wallet1) {
            this.name = name;
            this.age = age;
            this.wallet = Wallet1;
        }
    
        @Override
        public String toString() {
            return "Person1 [name=" + name + ", age=" + age + ", p=" + wallet + "]";
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Wallet1 getWallet1() {
            return wallet;
        }
    
        public void setWallet1(Wallet1 wallet) {
            this.wallet = wallet;
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            Person1 p =(Person1) super.clone() ;
            p.wallet = (Wallet1) wallet.clone() ;
            return p ;
        }
    
    }

    运行结果如下:

    Person1 [name=curry, age=28, p=Wallet1 [name=LV, money=10000]]
    Person1 [name=stu, age=30, p=Wallet1 [name=保罗, money=60]]

    深拷贝实例二

    package priv.jack.jdk.demo;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    /**
     * 深拷贝
     * @author Jack
     *
     */
    public class DeepCopy2{
    
        public static void main(String[] args) throws Exception {
            Wallet2 wallet = new Wallet2("LV", 10000);
            Person2 p1 = new Person2("curry", 28, wallet);
            Person2 p2 = (Person2) p1.deepClone();
            p2.getWallet2().setName("保罗");
            p2.getWallet2().setMoney(60);
            p2.setName("stu");
            p2.setAge(30);
            System.out.println(p1.toString());
            System.out.println(p2.toString());
        }
    
    }
    
    class Wallet2 implements Serializable {
        
        private static final long serialVersionUID = 5496170174896341934L;
        
        private String name;
        private long money;
    
        public Wallet2(String name, int money) {
            this.name = name;
            this.money = money;
        }
    
        @Override
        public String toString() {
            return "Wallet2 [name=" + name + ", money=" + money + "]";
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public long getMoney() {
            return money;
        }
    
        public void setMoney(long money) {
            this.money = money;
        }
    
    }
    
    class Person2 implements Serializable {
        
        private static final long serialVersionUID = -7860166025209047988L;
        
        private String name;
        private int age;
        private Wallet2 wallet;
    
        public Person2(String name, int age, Wallet2 Wallet2) {
            this.name = name;
            this.age = age;
            this.wallet = Wallet2;
        }
    
        @Override
        public String toString() {
            return "Person2 [name=" + name + ", age=" + age + ", p=" + wallet + "]";
        }
        
        public Object deepClone() throws Exception {
            ByteArrayOutputStream bao = new ByteArrayOutputStream() ;
            ObjectOutputStream oo = new ObjectOutputStream(bao) ;
            oo.writeObject(this);
            ByteArrayInputStream bai = new ByteArrayInputStream(bao.toByteArray()) ;
            ObjectInputStream oi = new ObjectInputStream(bai) ;
            Object obj = oi.readObject() ;
            return obj ;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Wallet2 getWallet2() {
            return wallet;
        }
    
        public void setWallet2(Wallet2 wallet) {
            this.wallet = wallet;
        }
    
    }

    运行结果如下:

    Person2 [name=curry, age=28, p=Wallet2 [name=LV, money=10000]]
    Person2 [name=stu, age=30, p=Wallet2 [name=保罗, money=60]]

      

    OK,done。

        

  • 相关阅读:
    eclips搭建python开发环境
    win7下odoo服务启动又停止解决方法
    ubuntu14.04调节屏幕亮度
    在ubunut下使用pycharm和eclipse进行python远程调试
    读书笔记——乔布斯,做最好的自己,共创式教练
    压力测试工具——Galting
    番茄工作法和Bullet Journal笔记法
    Openstack Swift中间件编写
    《黑客》读书笔记
    用腻了bootstrap的可以试试semantic-ui
  • 原文地址:https://www.cnblogs.com/jack2013/p/5200737.html
Copyright © 2020-2023  润新知