package yuwei.com;
//克隆
class Address{
String detail;
public Address(String detail) {
this.detail = detail;
}
}
//实现Cloneable接口
class User implements Cloneable
{
int age;
Address address;
public User(int age) {
this.age = age;
address = new Address("四叶草");
}
public User clone()
throws CloneNotSupportedException
{
return (User)super.clone();
}
}
public class TestDemo {
public static void main(String[] args) throws CloneNotSupportedException {
// TODO Auto-generated method stub
User u1 = new User(29);
//Clone得到u1对象的副本
User u2 = u1.clone();
//判断u1,u2是否相同
System.out.println(u1 == u2);
//判断u1,u2的address是否相同
System.out.println(u1.address == u2.address);
}
}
以上来自学习java疯狂讲义时的笔记