{"id":437274,"date":"2021-04-17T15:46:04","date_gmt":"2021-04-17T07:46:04","guid":{"rendered":"http:\/\/4563.org\/?p=437274"},"modified":"2021-04-17T15:46:04","modified_gmt":"2021-04-17T07:46:04","slug":"object-%e7%b1%bb%e4%b8%ad%e7%9a%84-clone-%e6%96%b9%e6%b3%95%e5%ba%94%e8%af%a5%e6%98%af-shallow-%e6%8b%b7%e8%b4%9d-%e9%82%a3%e5%ba%94%e8%af%a5%e6%98%af-p1-p2-%e6%8c%87%e5%90%91%e5%90%8c%e4%b8%80","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=437274","title":{"rendered":"Object \u7c7b\u4e2d\u7684 clone \u65b9\u6cd5\u5e94\u8be5\u662f shallow \u62f7\u8d1d \u90a3\u5e94\u8be5\u662f p1 p2 \u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61 \u4e3a\u4ec0\u4e48\u6211\u5b9e\u9645\u4f7f\u7528\u7684\u65f6\u5019\u5374\u4e0d\u5bf9?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Object \u7c7b\u4e2d\u7684 clone \u65b9\u6cd5\u5e94\u8be5\u662f shallow \u62f7\u8d1d \u90a3\u5e94\u8be5\u662f p1 p2 \u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61 \u4e3a\u4ec0\u4e48\u6211\u5b9e\u9645\u4f7f\u7528\u7684\u65f6\u5019\u5374\u4e0d\u5bf9?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gzk329 <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6d4b\u8bd5\u7c7b<\/p>\n<pre><code>public class TestCopy {     public static void main(String[] args) throws CloneNotSupportedException {         Person p1 = new Person(\"\u5f20\u4e09\", 18, new Address(\"\u6c5f\u82cf\u7701\", \"\u65e0\u9521\u5e02\"));         System.out.println(p1);         \/\/Person{pname='\u5f20\u4e09', page=18, addr=Address{province='\u6c5f\u82cf\u7701', city='\u65e0\u9521\u5e02'}}          Person p2 = (Person) p1.clone();         System.out.println(p1 + \" n\" + p2);         \/\/Person{pname='\u5f20\u4e09', page=18, addr=Address{province='\u6c5f\u82cf\u7701', city='\u65e0\u9521\u5e02'}}         \/\/Person{pname='\u5f20\u4e09', page=18, addr=Address{province='\u6c5f\u82cf\u7701', city='\u65e0\u9521\u5e02'}}          System.out.println(p1.equals(p2));         \/\/false          Address addr2 = new Address(\"\u6c5f\u82cf\u7701\", \"\u82cf\u5dde\u5e02\");         p2.setAddr(addr2);         System.out.println(\"\u4fee\u6539\u5730\u5740\u540e===========================================\");                  System.out.println(p1);         System.out.println(p2);         \/\/Person{pname='\u5f20\u4e09', page=18, addr=Address{province='\u6c5f\u82cf\u7701', city='\u65e0\u9521\u5e02'}}         \/\/Person{pname='\u5f20\u4e09', page=18, addr=Address{province='\u6c5f\u82cf\u7701', city='\u82cf\u5dde\u5e02'}}     } } <\/code><\/pre>\n<pre><code>\u6e90\u7801\u6ce8\u91ca\u4e5f\u5199\u7684\u5f88\u6e05\u695a Thus, this method performs a \"shallow copy\" of this object, not a \"deep copy\" operation. <\/code><\/pre>\n<pre><code>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(\"\u540d\u5b57\u662f:\" + this.pname + \",\u5e74\u9f84\u662f:\" + page + \",\u5730\u5740(\u7701+\u5e02)\u662f:\" + 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;     } } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5745591\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : misaka19000 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Person \u7684 equals \u662f\u4e0d\u662f\u88ab\u91cd\u5199\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745592\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cnxobo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6d45\u62f7\u8d1d\u7684\u7b80\u5355\u7406\u89e3\u4e3a\u521b\u5efa\u4e00\u4e2a\u65b0\u5b9e\u4f8b\u7136\u540e\u5c5e\u6027\u7528 = \u4e00\u4e2a\u4e00\u4e2a\u7684\u8d4b\u503c\u8fc7\u53bb\u3002<br \/>p1 p2 \u6307\u5411\u7684\u662f\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u4f46\u662f p1 p2 \u7684\u5f15\u7528\u7c7b\u578b\u5c5e\u6027\u6307\u5411\u7684\u5bf9\u8c61\u662f\u4e00\u6837\u7684\u3002<br \/>\u4e5f\u5c31\u662f p1.addr \u5c31\u662f p2.addr \u3002<br \/>\u5982\u679c\u4f60\u4e0d p2.setAddr\uff0c \u76f4\u63a5 p2.getAddr().setCity(&#8220;\u82cf\u5dde\u5e02&#8221;), \u90a3\u4e48 p1 \u548c p2 \u7684 addr city \u90fd\u4f1a\u53d8\u6210\u82cf\u5dde\u5e02\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745593\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : beichenhpy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u76f4\u63a5\u6253\u5370\u5185\u5b58\u5730\u5740\u770b\u770b\u4e0d\u5c31\u5b8c\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745594\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : gzk329 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cnxobo \u660e\u767d\u4e86 \u8c22\u8c22                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745595\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : beichenhpy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5e94\u8be5\u8981\u91cd\u5199 equals()\u548c hashCode()\u5427\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Object \u7c7b\u4e2d\u7684 clone &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/437274"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=437274"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/437274\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=437274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=437274"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=437274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}