java的对像克隆
public class Sheep implements Cloneable{
private String name;
public void setName(String arg) {
name = arg;
}
public String getName() {
return name;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
//克隆
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Sheep sheep = new Sheep(); //先得到那只羊的实例
sheep.setName("我是真的"); //给它做个记号
System.out.println("sheep.getName() = " + sheep.getName());
Sheep sheepClone = (Sheep)sheep.clone(); //开始克隆
System.out.println("sheepClone.getName() = " + sheepClone.getName());
}
}
//运行程序结果为:
//sheep.getName() = 我是真的
//sheepClone.getName() = 我是真的
private String name;
public void setName(String arg) {
name = arg;
}
public String getName() {
return name;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
//克隆
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Sheep sheep = new Sheep(); //先得到那只羊的实例
sheep.setName("我是真的"); //给它做个记号
System.out.println("sheep.getName() = " + sheep.getName());
Sheep sheepClone = (Sheep)sheep.clone(); //开始克隆
System.out.println("sheepClone.getName() = " + sheepClone.getName());
}
}
//运行程序结果为:
//sheep.getName() = 我是真的
//sheepClone.getName() = 我是真的