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