测试类
public class TestCopy {
public static void main(String[] args) throws CloneNotSupportedException {
Person p1 = new Person("张三", 18, new Address("江苏省", "无锡市"));
System.out.println(p1);
//Person{pname='张三', page=18, addr=Address{province='江苏省', city='无锡市'}}
Person p2 = (Person) p1.clone();
System.out.println(p1 + " \n" + p2);
//Person{pname='张三', page=18, addr=Address{province='江苏省', city='无锡市'}}
//Person{pname='张三', page=18, addr=Address{province='江苏省', city='无锡市'}}
System.out.println(p1.equals(p2));
//false
Address addr2 = new Address("江苏省", "苏州市");
p2.setAddr(addr2);
System.out.println("修改地址后===========================================");
System.out.println(p1);
System.out.println(p2);
//Person{pname='张三', page=18, addr=Address{province='江苏省', city='无锡市'}}
//Person{pname='张三', page=18, addr=Address{province='江苏省', city='苏州市'}}
}
}
源码注释也写的很清楚
Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
public class Person implements Cloneable{
private String pname;
private int page;
private Address addr;
public Person() {
}
public Person(String pname, int page, Address addr) {
this.pname = pname;
this.page = page;
this.addr = addr;
}
@Override
public String toString() {
return "Person{" +
"pname='" + pname + '\'' +
", page=" + page +
", addr=" + addr +
'}';
}
public void showAll(){
System.out.println("名字是:" + this.pname + ",年龄是:" + page + ",地址(省+市)是:" + addr.getProvince() + addr.getCity());
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
this.addr = addr;
}
}
public class Address{
private String province;
private String city;
public Address() {
}
public Address(String province, String city) {
this.province = province;
this.city = city;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.