Clone of objects in Java is implemented through the Cloneable interface. You must override the clone() method and explicitly throw CloneNotSupportedException. Cloning can be divided into deep copy and shallow copy: 1. Deep copy creates a complete copy of the object, including variable fields; 2. Shallow copy only copies the reference, and the original object and the copy share the same data.
#How is object cloning implemented in Java?
Introduction
In Java programming, cloning is used to create a copy of an object without modifying the original object. Cloning of objects can be used in various scenarios, such as creating multiple instances of an object, passing a copy of an object to avoid modifying the original object, and implementing deep and shallow copies.
Implementing Clone
Clone in Java is implemented by theCloneable
interface. Any class that wishes to implement cloning must implement this interface and override theclone()
method. Theclone()
method returns a clone of the object and must explicitly throwCloneNotSupportedException
if the class does not support cloning.
Deep copy and shallow copy
When cloning an object, two types of copies can be made: deep copy and shallow copy.
Practical case
Consider the followingStudent
class, which has three fields: name, age and address:
public class Student implements Cloneable { private String name; private int age; private Address address; @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } // 省略getter 和 setter 方法 }
Address
The class is also cloneable:
public class Address implements Cloneable { private String street; private String city; private String state; @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } // 省略getter 和 setter 方法 }
Now, consider the following code:
// 创建原始 Student 对象 Student originalStudent = new Student(); originalStudent.setName("John Doe"); originalStudent.setAge(21); Address originalAddress = new Address(); originalAddress.setStreet("123 Main Street"); originalStudent.setAddress(originalAddress); // 克隆原始Student对象 Student clonedStudent = (Student) originalStudent.clone(); // 修改克隆对象的字段 clonedStudent.setName("Jane Doe"); clonedStudent.setAge(22); Address clonedAddress = clonedStudent.getAddress(); clonedAddress.setStreet("456 Elm Street"); // 输出原始和克隆对象 System.out.println("Original Student:" + originalStudent); System.out.println("Cloned Student:" + clonedStudent);
The output will show:
Original Student:Student{name='John Doe', age=21, address=Address{street='123 Main Street', city=null, state=null}} Cloned Student:Student{name='Jane Doe', age=22, address=Address{street='456 Elm Street', city=null, state=null}}
In this In this case, since theAddress
class is cloneable, this is a deep copy. When you modify a cloned object's address field, it does not affect the original object's address field.
The above is the detailed content of How is object cloning implemented in Java?. For more information, please follow other related articles on the PHP Chinese website!