到底object.clone()是如何实现!!
都说object.clone()是"按位复制",但是object.clone()是如何知道关于子类的信息的呢?
如果是用"class.forName()"的话也是需要一个string的类名参数的呀.
各位高人请赐教object.clone()是如何实现的??
up有分!!
谢谢大家
都说object.clone()是"按位复制",但是object.clone()是如何知道关于子类的信息的呢?
如果是用"class.forName()"的话也是需要一个string的类名参数的呀.
各位高人请赐教object.clone()是如何实现的??
up有分!!
谢谢大家
另外,对于API提供的通用数据结构如Vector等,需要对其中的每个对象进行克隆。
如果是这样,也是需要查询常量池的吧?
但是super.clone()的调用只是调用根类的clone(),根类的clone()不知道子类的信息,怎么查询?比如
class A inplements CLoneable
{
int i=1;
char c="r";
public Object clone()
{
return super.clone()
}
}class B implements Cloneable
{
int j=2;
char k="t";
public Object clone()
{
return super.clone();
}
}
两个类同样调用super.clone()
根类是如何区分的呢??
所答非所问呀,老大!!!
object.clone() 复制了被包含对象的句柄或者说指针。
如果你要深层复制的话 就要自己在子类 覆写clone()
不过象Vector的clone()方法就不能自动尝试克隆Vector内包含的每个对象,所以象Vector这样的集合对象只能进行不能进行深层复制,这些都需要自己覆写。
// Checking to see if a reference can be cloned.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.// Can't clone this because it doesn't override clone():
class Ordinary {}// Overrides clone, but doesn't implement Cloneable:
class WrongClone extends Ordinary {
public Object clone() throws CloneNotSupportedException {
return super.clone(); // Throws exception
}
}// Does all the right things for cloning:
class IsCloneable extends Ordinary implements Cloneable {
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}// Turn off cloning by throwing the exception:
class NoMore extends IsCloneable {
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}class TryMore extends NoMore {
public Object clone() throws CloneNotSupportedException {
// Calls NoMore.clone(), throws exception:
return super.clone();
}
}class BackOn extends NoMore {
private BackOn duplicate(BackOn b) {
// Somehow make a copy of b and return that copy.
// This is a dummy copy, just to make the point:
return new BackOn();
}
public Object clone() {
// Doesn't call NoMore.clone():
return duplicate(this);
}
}// You can't inherit from this, so you can't override
// the clone method as you can in BackOn:
final class ReallyNoMore extends NoMore {}public class CheckCloneable {
public static Ordinary tryToClone(Ordinary ord) {
String id = ord.getClass().getName();
System.out.println("Attempting " + id);
Ordinary x = null;
if(ord instanceof Cloneable) {
try {
x = (Ordinary)((IsCloneable)ord).clone();
System.out.println("Cloned " + id);
} catch(CloneNotSupportedException e) {
System.err.println("Could not clone " + id);
}
} else {
System.out.println("Doesn't implement Cloneable");
}
return x;
}
public static void main(String[] args) {
// Upcasting:
Ordinary[] ord = {
new IsCloneable(),
new WrongClone(),
new NoMore(),
new TryMore(),
new BackOn(),
new ReallyNoMore(),
};
Ordinary x = new Ordinary();
// This won't compile; clone() is protected in Object:
//! x = (Ordinary)x.clone();
// Checks first to see if a class implements Cloneable:
for(int i = 0; i < ord.length; i++)
tryToClone(ord[i]);
}
}
{ public static void main(String[] args)
{
Employee emp = new Employee("kevin");
emp.address = new Address("Beijing");
System.out.println("name: " + emp.name + " age: " + emp.age + " Address: " + emp.address.city);
Employee empClone = (Employee) emp.clone();
empClone.age = 50;
empClone.name = "ding";
empClone.address.city = "Shanghai";
System.out.println("name: " + empClone.name +
" age: " + empClone.age
+" Address: " + emp.address.city);
System.out.println("name: " + emp.name + " age: " + emp.age + " Address: " + emp.address.city);
}
}class Employee implements Cloneable
{
String name;
int age;
Address address;
public Employee(String name)
{
this.name = name;
this.age = 30;
}
public Object clone()
{
try
{
return super.clone();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}class Address
{
String city;
public Address(String city)
{
this.city = city;
}
}
这段代码很好的说明了克隆的机制