首页 > 其他分享 >Object---clone方法

Object---clone方法

时间:2023-12-12 17:44:44浏览次数:25  
标签:code name clone Object list --- Person

 

概述

java.lang.Object#clone

 

By convention, the returned object should be obtained by calling {@code super.clone}.
If a class and all of its superclasses (except {@code Object}) obey this convention, it will be the case that {@code x.clone().getClass() == x.getClass()}.

按照约定,应该通过调用super.clone来获得新的克隆对象;

 

 

The method {@code clone} for class {@code Object} performs a specific cloning operation.
First, if the class of this object does not implement the interface {@code Cloneable}, then a {@code CloneNotSupportedException} is thrown.

Note that all arrays are considered to implement the interface {@code Cloneable} and that the return type of the {@code clone} method of an array type {@code T[]} is {@code T[]} where T is any reference or primitive type.

Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned.
Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

clone方法 执行clone操作;

如果 被克隆的对象没有实现Cloneable,将会抛出CloneNotSupportedException;

注意:所有的数组 都被认为实现了Cloneable

clone方法仅创建一个新的对象,对象的字段内容并没有clone;

clone方法是 浅拷贝,不是深拷贝;

 

The class {@code Object} does not itself implement the interface {@code Cloneable}, so calling the {@code clone} method on an object whose class is {@code Object} will result in throwing an exception at run time.

Object类 自身没有实现Cloneable,所以调用Object类的对象的clone将会抛出异常;

 

深拷贝&浅拷贝

浅拷贝

public static void main(String[] args) throws CloneNotSupportedException {
        List<String> list = Arrays.asList("a", "b");

        Person person = new Person("test", list);
        Object clone = person.clone();

        System.out.println(person == clone);


        int[] arr = {1,3,4};
        int[] clone1 = arr.clone();
        System.out.println(arr == clone1);


    }


    static class Person implements Cloneable{

        private String name;
        private List<String> list;

        Person(String name, List<String> list){
            this.name = name;
            this.list = list;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public static void test(){
            System.out.println("test...");
        }

        public List<String> getList() {
            return list;
        }

        public void setList(List<String> list) {
            this.list = list;
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

  

 

深拷贝

Person的clone方法深拷贝实现

@Override
        protected Object clone() throws CloneNotSupportedException {
            Person clone = (Person) super.clone();
            ArrayList<String> list = new ArrayList<>(this.list);
            clone.setList(list);
            return clone;
        }

  

 

标签:code,name,clone,Object,list,---,Person
From: https://www.cnblogs.com/anpeiyong/p/17897420.html

相关文章

  • 进/线/协程--引自阿秀的学习笔记
    进程、线程与协程区别1、进程是资源分配的基本单位,运行一个可执行程序会创建一个或多个进程,进程就是运行起来的可执行程序2、线程是资源调度的基本单位,也是程序执行的基本单位,是轻量级的进程。每个进程中都有唯一的主线程,且只能有一个,主线程和进程是相互依存的关系,主线程结束进......
  • centos7搭建kubernetes-v1.25.1集群(Containerd作为运行时)
    集群配置节点名称内存硬盘处理器内核总数ipmaster6GB40GB6192.168.67.166node16GB40GB6192.168.67.167node26GB40GB6192.168.67.168一、所有节点更改镜像源curl-o/etc/yum.repos.d/CentOS-Base.repo二、所有节点安装docker,注意:K8s在1.24以上......
  • [NOIP2010 提高组] 关押罪犯 - 洛谷
    P1525[NOIP2010提高组]关押罪犯-洛谷|计算机科学教育新生态(luogu.com.cn)种类并查集#include<bits/stdc++.h>#definedebug(a)cout<<#a<<"="<<a<<'\n';usingnamespacestd;usingi64=longlong;typedefpair<i64,i64>......
  • .net批量插入到oracle数据库--三种方式对比,亲测
    根据上篇的内容,简单测试了三种方式的对比,需要使用的,请自行根据需求优化。。。上篇文字网址:https://www.cnblogs.com/ggll611928/p/17897005.html 1、创建测试表CREATETABLET_TEST(IDNUMBER(9)notnull,NAMENVARCHAR2(30)notnull,AGENUMBER(2)......
  • 无涯教程-MFC - Tree Control函数
    TreeViewControl是一个窗口,其中显示项目的层次结构列表,例如文档中的标题,索引中的条目或磁盘上的文件和目录,每个项目都包含一个标签和一个可选的位图图像,并且每个项目都可以具有与其相关联的子项目列表,通过单击一个项目,用户可以展开和折叠子项目的关联列表,它由CTreeCtrl类表......
  • 表空间传输TTS(RAC-to-单机)
    环境:OS:Centos7DB:12.2.0.1源库:2节点RAC目的库:单节点 1.源库创建表空间createtablespacetps_testloggingdatafile'+DATA'size50mautoextendonnext10mmaxsize2048mextentmanagementlocalsegmentspacemanagementauto;createtablespacetps_test01......
  • elasticsearch安装-集群
    下载安装包国内镜像,速度非常快https://mirrors.huaweicloud.com/elasticsearch/https://mirrors.huaweicloud.com/kibana/wgethttps://mirrors.huaweicloud.com/elasticsearch/7.9.3/elasticsearch-7.9.3-linux-x86_64.tar.gz安装准备3台机器:1、安装目录:/usr/local/elas......
  • 2023年12月DAMA-CDGP数据治理专家认证招生简章
    DAMA认证为数据管理专业人士提供职业目标晋升规划,彰显了职业发展里程碑及发展阶梯定义,帮助数据管理从业人士获得企业数字化转型战略下的必备职业能力,促进开展工作实践应用及实际问题解决,形成企业所需的新数字经济下的核心职业竞争能力。DAMA是数据管理方面的认证,帮助数据从业者提升......
  • 【flink番外篇】3、fflink的source(内置、mysql、kafka、redis、clickhouse)介绍及示例(2
    Flink系列文章一、Flink专栏Flink专栏系统介绍某一知识点,并辅以具体的示例进行说明。1、Flink部署系列本部分介绍Flink的部署、配置相关基础内容。2、Flink基础系列本部分介绍Flink的基础部分,比如术语、架构、编程模型、编程指南、基本的datastreamapi用法、四大基......
  • 【flink番外篇】3、flink的source(内置、mysql、kafka、redis、clickhouse)介绍及示例(3)-
    Flink系列文章一、Flink专栏Flink专栏系统介绍某一知识点,并辅以具体的示例进行说明。1、Flink部署系列本部分介绍Flink的部署、配置相关基础内容。2、Flink基础系列本部分介绍Flink的基础部分,比如术语、架构、编程模型、编程指南、基本的datastreamapi用法、四大基......