class guodong{
String name;
public guodong(String name){
this.name = name;
}
}
class info implements Cloneable{
int age = 22;
guodong Address ;
public info(int a){
this.age = a;
this.Address = new guodong("guodong");
}
public info clone() throws CloneNotSupportedException
{
//通过Object类的clone的方法来克隆出一个info的副本。
return (info)super.clone();
}
}
public class CloneTest {
public static void main(String[] args) throws CloneNotSupportedException {
info guo = new info(22);
info guoguo = guo.clone(); //在guoguo中的到了guo的副本
//通过对比guo和guoguo 来判断是否是同一个类,得到false
System.out.println(guo == guoguo );
//来判断guo中的Address类和guoguo中的Address类得到true
System.out.println(guo.Address == guoguo.Address);
//结论:通过clone克隆得到的实例与原实例并不是同一个实例,在堆内存中占据不同的空间,但是在实例中的引用所指向的地址却是同一个地址。
}
}
java克隆类
最新推荐文章于 2024-05-23 18:18:25 发布