Home  >  Article  >  Java  >  Summary of Java-clone code examples

Summary of Java-clone code examples

黄舟
黄舟Original
2017-03-15 11:44:151439browse

What you learn on paper is shallow, but you know you have to do it in detail --Lu You Ask the canal how clear it is to have a source of living water --Zhu Xi


clone and Copy: When there is a objectPerson p1=new Person("name",age );Person p2=p1;At this time, it is just a simple copyReference, both p1 and p2 point to the same object in the memory, modifying either p1 or p2 will affect the other party. When the parameter of the function is an object, the reference type is passed, and the operations inside the object function will affect the external object. If you want to pass a copy of the object, you need to use the clone() method of object. ) The copy object returns a new object, not a reference; the difference between the copy object and the new object returned by the new operator is that the copy object already contains some information of the original object. , rather than the initial information of the object. When to use shallow Clone (shallow copy, shadow copy) , when to use deep Clone (deep copy) : It mainly depends on the nature of the specific domain object. If it is a basic type, use shallow Clone, if reference variable (reference variable) uses deep Clone.

Steps to implement cloning:

(1) Implement Cloneable interface. Cloneable does not have any abstract methods, which is called the flag interface.

(2) Override the clone method of the base class and declare it as public

(3) Override the clone method in the derived class and call super.clone( )

is divided into shallow copy and deep copy in clone. Shallow copy means calling super.clone() directly regardless of whether the parameter is a basic type or a reference type. Copy, the resulting reference type still points to the same memory; deep copy means that after calling super.clone(), the clone() function of the reference parameter must also be called to copy, to achieve deep copy.

Shallow copy:

(1) Basic data type (String) Will a new object be copied?

(2) Reference object or the same object.

Shallow copy sample code:

public class Info {
    public int idnumber;
    public int getIdnumber() {
    return idnumber;
    }
    public void setIdnumber(int idnumber) {
        this.idnumber = idnumber;
    }
}
public class Person  implements Cloneable{
    private int age;
    private String nameString;
    public Info info;
    public Info getInfo() {
        return info;
    }
    public void setInfo(Info info) {
        this.info = info;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getNameString() {
        return nameString;
    }
    public void setNameString(String nameString) {
        this.nameString = nameString;
    }
    public String toString()
    {
        return "name:"+nameString+",age:"+age+",idnumber"+info.idnumber;
    }
    public Object clone()
    {
        Person person=null;
        try {
            person=(Person)super.clone(); //浅拷贝
            //person.info=(Info)info.clone();
        } catch (CloneNotSupportedException e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
        }
        return person;
    }
}

public class Hello {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Info info=new Info();
info.setIdnumber(100);
Person person1=new Person();
person1.setAge(10);
person1.setNameString("tiantian");
person1.info=info;
Person person2=(Person)person1.clone();
person2.setNameString("hello");
person2.setAge(20);
person2.info.setIdnumber(200);
System.out.println("person2"+person2);
System.out.println("person1"+person1);
}
}

The final result is:

person2name:hello,age:20,idnumber200
person1name:tiantian,age:10,idnumber200

The basic types name and age of person1 and person2 have changed, but the reference type The value of idnumber has not changed.

Deep copy:

(1)对存在的引用类型也进行拷贝,浅拷贝与深拷贝的代码还是有不同点的。


深拷贝代码示例:

public class Info implements Cloneable {//Info也进行拷贝
    public int idnumber;
    public int getIdnumber() {
        return idnumber;
    }
    public void setIdnumber(int idnumber) {
        this.idnumber = idnumber;
    }
    public Object clone()
    {
        Info info=null;
        try {
            info=(Info)super.clone();
        } catch (CloneNotSupportedException e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
        }
        return info;
    }
}

public class Person  implements Cloneable{

private int age;
    private String nameString;
    public Info info;

    public Info getInfo() {
        return info;
    }
    public void setInfo(Info info) {
        this.info = info;
    }
    public Person() {
        // TODO 自动生成的构造函数存根

    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getNameString() {
        return nameString;
    }
    public void setNameString(String nameString) {
        this.nameString = nameString;
    }

    public String toString()
    {
        return "name:"+nameString+",age:"+age+",idnumber"+info.idnumber;
    }

    public Object clone()
    {
        Person person=null;
        try {
            person=(Person)super.clone(); //浅拷贝
            person.info=(Info)info.clone();//加上这一句就变成了深拷贝,对对象引用也进行一次拷贝
            } catch (CloneNotSupportedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
            }
        return person;
    }
}

结果:

person2name:hello,age:20,idnumber200
person1name:tiantian,age:10,idnumber100

所有的值都进行了改变。

The above is the detailed content of Summary of Java-clone code examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn