深拷贝

来源:互联网 发布:网络销售总监 编辑:程序博客网 时间:2024/05/01 18:43
class DepthReading implements Cloneable{
private double depth;
public DepthReading(double depth) {
this.depth = depth;
}
public Object clone(){
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
class TemperatureReading implements Cloneable{
private long time;
private double temperature;
public TemperatureReading(double temperature) {
time = System.currentTimeMillis();
this.temperature = temperature;
}
public Object clone(){
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
class OceanReading implements Cloneable{
private DepthReading depth;
private TemperatureReading temperature;
public OceanReading(double tdata, double ddate) {
temperature = new TemperatureReading(tdata);
depth = new DepthReading(ddate);
}
public Object clone(){
OceanReading o = null;
try {
o = (OceanReading)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
o.depth = (DepthReading)o.depth.clone();
o.temperature = (TemperatureReading)o.temperature.clone();
return o;

}
public class DeepCopy {
public static void main(String[] args) {
OceanReading reading = new OceanReading(33.9, 100.5);
OceanReading r = (OceanReading)reading.clone();
}
}
0 0