static class Test implements Cloneable {
private ArrayList<String> strings = new ArrayList<>();
public void addString(String s) {
protected Object clone() throws CloneNotSupportedException {
Test test = (Test) super.clone();
// 然后手工复制其中的值到新的对象中以实现深拷贝
test.strings = new ArrayList<>();
for (String s : strings) {
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (String s : strings) {
stringBuilder.append(s).append(" ");
return stringBuilder.toString();
public static void main(String[] args) throws CloneNotSupportedException {
Test t2 = (Test) t1.clone(); // 深拷贝