Object---clone方法

 

概述

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将会抛出异常;

 

深拷贝&浅拷贝

浅拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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();
        }
    }

  

 

深拷贝

1
2
3
4
5
6
7
8
9
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;
        }

  

 

posted on   anpeiyong  阅读(6)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-12-12 Kafka---系统学习

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示