1、Car.java
package com.ssm.cts.pattern.prototype; import java.util.ArrayList; public class Car implements Cloneable { private ArrayList<String> arrayList = new ArrayList<String>(); @Override public Car clone() { Car car = null; try { car = (Car) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return car; } public void setValue(String value) { this.arrayList.add(value); } public ArrayList<String> getValue() { return this.arrayList; } }
2、Demo.java
package com.ssm.cts.pattern.prototype; public class Demo { // 测试 public static void main(String[] args) { // 生成对象 Car car = new Car(); car.setValue("别克"); // 复制对象 Car cloneThing = car.clone(); cloneThing.setValue("奥迪"); System.out.println(car.getValue()); } }