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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。