- 泛型对象实现
Cloneable
接口 - 重写泛型对象的
clone
方法 - 使用的时候调用 泛型对象的
clone();
方法
代码实例:
public class Account implements Cloneable {
public String name;
public String pwd;
public String serviceAddress;
@Override
public Account clone() throws CloneNotSupportedException {
return ((Account) super.clone());
}
@Override
public String toString() {
return "Account{" +
"name='" + name + '\'' +
", pwd='" + pwd + '\'' +
", serviceAddress='" + serviceAddress + '\'' +
'}';
}
}
/**********************************************************/
//调用的时候
List<Account> new_l = new ArrayList<>();
try {
for (Account copy : src) {
new_l.add(copy.clone());
}
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}