protected Object clone()
throws CloneNotSupportedException
//原始类型 int a = 1; int b = a; //引用类型 String[] weekdays = new String[5]; String[] gongzuori = weekdays;//仅拷贝引用
public interface Cloneable {
}
static class CloneableImp implements Cloneable {
public int count;
public Child child;
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
CloneableImp imp1 = new CloneableImp();
imp1.child = new Child("Andy");
try {
Object obj = imp1.clone();
CloneableImp imp2 = (CloneableImp)obj;
System.out.println("main imp2.child.name=" + imp2.child.name);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
CloneableImp imp1 = new CloneableImp();
imp1.child = new Child("Andy");
try {
Object obj = imp1.clone();
CloneableImp imp2 = (CloneableImp)obj;
imp2.child.name = "Bob";
System.out.println("main imp1.child.name=" + imp1.child.name);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
main imp1.child.name=Bob
public class Child implements Cloneable{
public String name;
public Child(String name) {
this.name = name;
}
@Override
public String toString() {
return "Child [name=" + name + "]";
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
static class CloneableImp implements Cloneable {
public int count;
public Child child;
@Override
public Object clone() throws CloneNotSupportedException {
CloneableImp obj = (CloneableImp)super.clone();
obj.child = (Child) child.clone();
return obj;
}
}
public class Car {
Wheel wheel;
String manufacturer;
public Car(Wheel wheel, String manufacturer) {
this.wheel = wheel;
this.manufacturer = manufacturer;
}
//copy constructor
public Car(Car car) {
this(car.wheel, car.manufacturer);
}
public static class Wheel {
String brand;
}
}
//copy constructor
public Car(Car car) {
Wheel wheel = new Wheel();
wheel.brand = car.wheel.brand;
this.wheel = wheel;
this.manufacturer = car.manufacturer;
}
public static Car newInstance(Car car) {
return new Car(car);
}
public class DeepCopyExample implements Serializable{
private static final long serialVersionUID = 6098694917984051357L;
public Child child;
public DeepCopyExample copy() {
DeepCopyExample copy = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
copy = (DeepCopyExample) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return copy;
}
}
public class Child implements Serializable{
private static final long serialVersionUID = 6832122780722711261L;
public String name = "";
public Child(String name) {
this.name = name;
}
@Override
public String toString() {
return "Child [name=" + name + "]";
}
}
DeepCopyExample example = new DeepCopyExample();
example.child = new Child("Example");
DeepCopyExample copy = example.copy();
if (copy != null) {
copy.child.name = "Copied";
System.out.println("example.child=" + example.child + ";copy.child=" + copy.child);
}
//输出结果:example.child=Child [name=Example];copy.child=Child [name=Copied]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有