-------------------------------浅拷贝-----------------------------------------
@Data
public class ShopItem implements Cloneable {
private String shopItemName;
public ShopItem clone() throws CloneNotSupportedException {
ShopItem clone = (ShopItem) super.clone();
return clone;
}
}
@Data
public class Shop implements Cloneable {
private String shopName;
private Integer shopNumber;
private ShopItem shopItem;
public Shop(String shopName, Integer shopNumber, ShopItem shopItem) {
this.shopName = shopName;
this.shopNumber = shopNumber;
this.shopItem = shopItem;
}
public static void main(String[] args) throws CloneNotSupportedException {
ShopItem shopItem = new ShopItem();
shopItem.setShopItemName("shopItemName1");
Shop shop = new Shop("shopName1", 10, shopItem);
Shop clone = shop.clone();
System.out.println("shop: " + shop);
System.out.println("clone: " + clone);
System.out.println("-----------------");
shopItem.setShopItemName("shopItemName2");
System.out.println("shop: " + shop);
System.out.println("clone: " + clone);
}
public Shop clone() throws CloneNotSupportedException {
Shop shop = (Shop) super.clone();
// ShopItem clone = this.shopItem.clone();
// shop.setShopItem(clone);
return shop;
}
}
打印结果:
shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
-----------------
shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName2))
clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName2))
------------------------------------------------------深拷贝------------------------------------------------------------------------------
@Data
public class ShopItem implements Cloneable {
private String shopItemName;
public ShopItem clone() throws CloneNotSupportedException {
ShopItem clone = (ShopItem) super.clone();
return clone;
}
}
@Data
public class Shop implements Cloneable {
private String shopName;
private Integer shopNumber;
private ShopItem shopItem;
public Shop(String shopName, Integer shopNumber, ShopItem shopItem) {
this.shopName = shopName;
this.shopNumber = shopNumber;
this.shopItem = shopItem;
}
public static void main(String[] args) throws CloneNotSupportedException {
ShopItem shopItem = new ShopItem();
shopItem.setShopItemName("shopItemName1");
Shop shop = new Shop("shopName1", 10, shopItem);
Shop clone = shop.clone();
System.out.println("shop: " + shop);
System.out.println("clone: " + clone);
System.out.println("-----------------");
shopItem.setShopItemName("shopItemName2");
System.out.println("shop: " + shop);
System.out.println("clone: " + clone);
}
public Shop clone() throws CloneNotSupportedException {
Shop shop = (Shop) super.clone();
ShopItem clone = this.shopItem.clone();
shop.setShopItem(clone);
return shop;
}
}
打印结果:
shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
-----------------
shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName2))
clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
------------------------------------------------------------反序列化方式----------------------------------------------------------------
@Data
public class Shop implements Cloneable, Serializable {
private String shopName;
private Integer shopNumber;
private ShopItem shopItem;
public Shop(String shopName, Integer shopNumber, ShopItem shopItem) {
this.shopName = shopName;
this.shopNumber = shopNumber;
this.shopItem = shopItem;
}
public static void main(String[] args) throws CloneNotSupportedException {
ShopItem shopItem = new ShopItem();
shopItem.setShopItemName("shopItemName1");
Shop shop = new Shop("shopName1", 10, shopItem);
Shop clone = shop.clone();
System.out.println("shop: " + shop);
System.out.println("clone: " + clone);
System.out.println("-----------------");
shopItem.setShopItemName("shopItemName2");
System.out.println("shop: " + shop);
System.out.println("clone: " + clone);
}
public Shop clone() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(this);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try(ObjectInputStream oos = new ObjectInputStream(bais)) {
Shop shop = (Shop) oos.readObject();
return shop;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
@Data
public class ShopItem implements Cloneable, Serializable {
private String shopItemName;
// public ShopItem clone() throws CloneNotSupportedException {
// ShopItem clone = (ShopItem) super.clone();
// return clone;
// }
}
打印结果:
shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))
-----------------
shop: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName2))
clone: Shop(shopName=shopName1, shopNumber=10, shopItem=ShopItem(shopItemName=shopItemName1))