From b45b55167a0b1cde3ceb42d4a305bc9a0daed665 Mon Sep 17 00:00:00 2001 From: bingor_yhj Date: Tue, 18 Oct 2022 23:18:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98(=E4=B8=80?= =?UTF-8?q?=E6=9C=9F)-=E8=AE=BE=E8=AE=A1=E6=A8=A1=E5=BC=8F=E7=89=88-protot?= =?UTF-8?q?ype=E6=A8=A1=E5=9E=8B=EF=BC=88=E4=B8=8D=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E7=89=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/msb/prototype/v1/Person.java | 53 +++++++++++++++++ .../java/com/msb/prototype/v2/Person.java | 59 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/main/java/com/msb/prototype/v1/Person.java create mode 100644 src/main/java/com/msb/prototype/v2/Person.java diff --git a/src/main/java/com/msb/prototype/v1/Person.java b/src/main/java/com/msb/prototype/v1/Person.java new file mode 100644 index 0000000..523b618 --- /dev/null +++ b/src/main/java/com/msb/prototype/v1/Person.java @@ -0,0 +1,53 @@ +package com.msb.prototype.v1; + +/** + * @Author bingor + * @Date 2022-10-18 22:25 + * @Description: com.msb.prototype.v1 + * @Version: 1.0 + */ +public class Person implements Cloneable { + int id; + String name; + Location location; + + public Person(int id, String name, Location location) { + this.id = id; + this.name = name; + this.location = location; + } + + @Override + protected Object clone() throws CloneNotSupportedException { + Person clone = (Person) super.clone(); + return clone; + } +} + +class Location { + String street; + String roomNo; + + public Location(String street, String roomNo) { + this.street = street; + this.roomNo = roomNo; + } + + @Override + public String toString() { + return "Location{" + + "street='" + street + '\'' + + ", roomNo='" + roomNo + '\'' + + '}'; + } +} + +class Main { + public static void main(String[] args) throws CloneNotSupportedException { + Location location = new Location("机场路", "8号"); + Person person1 = new Person(1, "bingor", location); + Person person2 = (Person) person1.clone(); + person1.location.street = "龙水路"; + System.out.println(person2.location); + } +} \ No newline at end of file diff --git a/src/main/java/com/msb/prototype/v2/Person.java b/src/main/java/com/msb/prototype/v2/Person.java new file mode 100644 index 0000000..e2b7fc7 --- /dev/null +++ b/src/main/java/com/msb/prototype/v2/Person.java @@ -0,0 +1,59 @@ +package com.msb.prototype.v2; + +/** + * @Author bingor + * @Date 2022-10-18 22:25 + * @Description: com.msb.prototype.v1 + * @Version: 1.0 + */ +public class Person implements Cloneable { + int id; + String name; + Location location; + + public Person(int id, String name, Location location) { + this.id = id; + this.name = name; + this.location = location; + } + + @Override + protected Object clone() throws CloneNotSupportedException { + Person clone = (Person) super.clone(); + clone.location = (Location) location.clone(); + return clone; + } +} + +class Location implements Cloneable { + String street; + String roomNo; + + public Location(String street, String roomNo) { + this.street = street; + this.roomNo = roomNo; + } + + @Override + protected Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + @Override + public String toString() { + return "Location{" + + "street='" + street + '\'' + + ", roomNo='" + roomNo + '\'' + + '}'; + } +} + +class Main { + public static void main(String[] args) throws CloneNotSupportedException { + Location location = new Location("机场路", "8号"); + Person person1 = new Person(1, "bingor", location); + Person person2 = (Person) person1.clone(); + person1.location.street = "龙水路"; + System.out.println(person2.location); + } +} \ No newline at end of file