Java.lang.Cloneable接口是一个Marker。它是在 JDK 1.0 中引入的。有一个方法clone()在对象类中。可克隆接口是由类来实现的对象.clone()方法有效,从而使 field-for-field 复制。该接口允许实现类克隆其对象,而不是使用新的操作符。
声明
public interface Cloneable
示例 1:下面的程序解释了如果你尝试克隆一个没有实现 Cloneable 接口的对象,它会CloneNotSupportedException,您可能想要处理。
Java
// Java program to Demonstrate the
// application of Cloneable interface
import java.io.*;
import java.util.*;
class Student {
// attributes of Student class
String name = null;
int id = 0;
// default constructor
Student() {}
// parameterized constructor
Student(String name, int id)
{
this.name = name;
this.id = id;
}
public static void main(String[] args)
{
// create an instance of Student
Student s1 = new Student("Ashish", 121);
// Try to clone s1 and assign
// the new object to s2
Student s2 = s1.clone();
}
}
输出:
prog.java:28: error: incompatible types: Object cannot be converted to Student Student s2 = s1.clone(); ^ 1 error
示例 2:下面的代码解释了如何正确使用 Cloneable 接口来使 Object.clone() 方法合法。实现此接口的类应重写 Object.clone() 方法(受保护),以便可以调用它。
Java
// Java program to illustrate Cloneable interface
import java.lang.Cloneable;
// By implementing Cloneable interface
// we make sure that instances of class A
// can be cloned.
class A implements Cloneable {
int i;
String s;
// A class constructor
public A(int i, String s)
{
this.i = i;
this.s = s;
}
// Overriding clone() method
// by simply calling Object class
// clone() method.
@Override
protected Object clone()
throws CloneNotSupportedException
{
return super.clone();
}
}
public class Test {
public static void main(String[] args)
throws CloneNotSupportedException
{
A a = new A(20, "GeeksForGeeks");
// cloning 'a' and holding
// new cloned object reference in b
// down-casting as clone() return type is Object
A b = (A)a.clone();
System.out.println(b.i);
System.out.println(b.s);
}
}
20 GeeksForGeeks
使用clone()方法进行深度复制
深度对象克隆就像通过将字段从原始对象复制到克隆对象来创建原始对象的精确副本一样。为复制原始对象内容的克隆对象分配单独的内存。clone()方法可以创建浅层和深拷贝基于原始对象的实现。深拷贝创建一个新的内存,其内容与原始对象相同。这就是为什么当我们在克隆后更改原始对象的内容时,这些更改不会反映在克隆对象中。副本有多种类型,例如深深浅浅,和惰性复制。下面的代码解释了使用clone()方法的深层复制。
Java
// A Java program to demonstrate deep copy
// using clone()
import java.util.ArrayList;
// An object reference of this class is
// contained by Test2
class Test {
int x, y;
}
// Contains a reference of Test and implements
// clone with deep copy.
class Test2 implements Cloneable {
int a, b;
Test c = new Test();
public Object clone() throws CloneNotSupportedException
{
// Assign the shallow copy to new reference variable
// t
Test2 t = (Test2)super.clone();
t.c = new Test();
// Create a new object for the field c
// and assign it to shallow copy obtained,
// to make it a deep copy
return t;
}
}
public class Main {
public static void main(String args[])
throws CloneNotSupportedException
{
Test2 t1 = new Test2();
t1.a = 10;
t1.b = 20;
t1.c.x = 30;
t1.c.y = 40;
Test2 t3 = (Test2)t1.clone();
t3.a = 100;
// Change in primitive type of t2 will not
// be reflected in t1 field
t3.c.x = 300;
// Change in object type field of t2 will not
// be reflected in t1(deep copy)
System.out.println(t1.a + " " + t1.b + " " + t1.c.x
+ " " + t1.c.y);
System.out.println(t3.a + " " + t3.b + " " + t3.c.x
+ " " + t3.c.y);
}
}
10 20 30 40 100 20 300 0
注意:该接口不包含clone方法。因此,不可能仅凭借实现此接口的事实来克隆对象。即使以反射方式调用克隆方法,也不能保证它会成功。
参考:https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Cloneable.html
相关用法
- Java CloneNotSupportedException用法及代码示例
- Java Clock equals()用法及代码示例
- Java Clock fixed()用法及代码示例
- Java Clock getZone()用法及代码示例
- Java Clock hashCode()用法及代码示例
- Java Clock instant()用法及代码示例
- Java Clock millis()用法及代码示例
- Java Clock offset()用法及代码示例
- Java Clock system()用法及代码示例
- Java Clock systemDefaultZone()用法及代码示例
- Java Clock systemUTC()用法及代码示例
- Java Clock tick()用法及代码示例
- Java Clock tickSeconds()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
- Java Clock withZone()用法及代码示例
- Java Closures用法及代码示例
- Java ClosedChannelException用法及代码示例
- Java Closeable用法及代码示例
- Java ClassLoader.clearAssertionStatus()用法及代码示例
- Java ClassLoader.getSystemResource()用法及代码示例
- Java Class asSubclass()用法及代码示例
- Java Class getSuperClass()用法及代码示例
- Java Class isAnonymousClass()用法及代码示例
- Java Class isPrimitive()用法及代码示例
- Java Class newInstance()用法及代码示例
注:本文由纯净天空筛选整理自Ganeshchowdharysadanala大神的英文原创作品 Cloneable Interface in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。