首页 > 编程语言 >Java 6.16 DeepClone and ShallowClone

Java 6.16 DeepClone and ShallowClone

时间:2024-06-17 09:32:39浏览次数:23  
标签:city Java Address 6.16 System DeepClone Person address out

  • 浅克隆:复制对象的引用地址,导致克隆对象和原始对象共享引用类型字段的实际对象。
  • class Person implements Cloneable {
        String name;
        Address address;
        
        public Person(String name, Address address) {
            this.name = name;
            this.address = address;
        }
        
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
    
    class Address {
        String city;
        
        public Address(String city) {
            this.city = city;
        }
    }
    
    public class Main {
        public static void main(String[] args) throws CloneNotSupportedException {
            Address address = new Address("New York");
            Person person1 = new Person("Alice", address);
            Person person2 = (Person) person1.clone();
            
            System.out.println(person1.name); // Alice
            System.out.println(person2.name); // Alice
            
            System.out.println(person1.address.city); // New York
            System.out.println(person2.address.city); // New York
            
            person2.address.city = "Los Angeles";
            
            System.out.println(person1.address.city); // Los Angeles (说明 address 引用被共享)
            System.out.println(person2.address.city); // Los Angeles
        }
    }

  • 深克隆:复制引用类型字段所指向的实际对象,使得克隆对象和原始对象完全独立。
  • class Person implements Cloneable {
        String name;
        Address address;
        
        public Person(String name, Address address) {
            this.name = name;
            this.address = address;
        }
        
        @Override
        protected Object clone() throws CloneNotSupportedException {
            Person cloned = (Person) super.clone();
            cloned.address = (Address) address.clone(); // 深拷贝 address
            return cloned;
        }
    }
    
    class Address implements Cloneable {
        String city;
        
        public Address(String city) {
            this.city = city;
        }
        
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
    
    public class Main {
        public static void main(String[] args) throws CloneNotSupportedException {
            Address address = new Address("New York");
            Person person1 = new Person("Alice", address);
            Person person2 = (Person) person1.clone();
            
            System.out.println(person1.name); // Alice
            System.out.println(person2.name); // Alice
            
            System.out.println(person1.address.city); // New York
            System.out.println(person2.address.city); // New York
            
            person2.address.city = "Los Angeles";
            
            System.out.println(person1.address.city); // New York (说明 address 引用被独立复制)
            System.out.println(person2.address.city); // Los Angeles
        }
    }

标签:city,Java,Address,6.16,System,DeepClone,Person,address,out
From: https://blog.csdn.net/k1076731326/article/details/139721263

相关文章

  • Java - function
     Java-Assignment04(100pts)InstructionsWriteeachexerciseinitsownmethod.Uncommentthefunctioncallsinmain()toactivateeachexercise.Referto"Assignment01"ifnecessary.Foreachexercise,usecommentstowritepseudocodew......
  • 面试官:Java中缓冲流真的性能很好吗?我看未必
    一、写在开头上一篇文章中,我们介绍了JavaIO流中的4个基类:InputStream、OutputStream、Reader、Writer,那么这一篇中,我们将以四个基类所衍生出来,应对不同场景的数据流进行学习。二、衍生数据流分类我们上面说了java.io包中有40多个类,都从InputStream、OutputStream、Reader、Wr......
  • 第二章JAVA的第一个开发程序
    1.JAVA的源文件以:.java结尾2.class:类,代码容器class类名Hello{  publicstaticvoidmain(String[]args){  System.out.println("Hello,world");  }}(1)main函数:被称为主函数,程序的入口,一个类中最多只能有一个主函数(2)System.out.println();//控制台打印出......
  • 5 个 JavaScript 自定义的实用函数
    嘿!......
  • 2024.6.16
    publicclassSparkSQL09_Source_Req{publicstaticvoidmain(String[]args){//TODO在编码前,设定Hadoop的访问用户System.setProperty("HADOOP_USER_NAME","atguigu");finalSparkSessionsparkSession=SparkSession......
  • Java高手的30k之路|面试宝典|精通Map篇
    HashMapHashMap是Java集合框架中非常重要的一部分,它是基于哈希表的数据结构。1.基于哈希表的实现HashMap基于哈希表实现。哈希表是通过将键(Key)映射到值(Value)的一种数据结构。具体来说,HashMap使用一个数组和链表(在冲突较少时)或红黑树(在冲突较多时)来存储元素。2.负......
  • 【Java基础】输入输出流(IO流)
     目录一、流的概念二、输入输出流的类层次结构图三、使用 InputStream 和 OutputStream流类四、使用Reader和Writer流类Java语言的输入输出功能必须借助于输入输出包java.io来实现,Java开发环境提供了丰富的流类,完成从基本的输人输出到文件操作。利用java.io......
  • JAVA 程序 在 cmd 窗口的运行
    1、在指定路径创建文件:eg文件名称:Hello.java;2、在文件内部输入内容:publicclassHello{publicstaticvoidmain(String[]args){System.out.println("Helloworld!");}}3、在cmd运行命令javac+文件名称Hello.java4、生成Hello.class......
  • Java基础:B树、B+树和红黑树的数据结构,三者区别
    B树(B-Tree)数据结构节点结构:每个节点包含多个键值和子节点指针。阶(Degree):B树的阶定义了每个节点的最小和最大键值数。对于阶为(m)的B树:每个节点最多有(m-1)个键值和(m)个子节点。每个节点(除了根节点)至少有(\lceilm/2\rceil-1)个键值和(\lceilm/......
  • 6.16-二叉树的层序遍历~
    429.N叉树的层序遍历题意描述:给定一个N叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。例如,给定一个3叉树:返回其层序遍历:[[1],[3,2,4],[5,6]]思路:AC代码:classSolution{public:vector<vector<int>>levelOrder(Node*root){queue<No......