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(System.err);
}
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(System.err);
}
return o;
}
}
class OceanReading implements Cloneable{
private DepthReading depth;
private TemperatureReading temperature;
public OceanReading(double tdata,double ddata){
temperature=new TemperatureReading(tdata);
depth=new DepthReading(ddata);
}
public Object clone(){
Object o=null;
try{
o=(OceanReading)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace(System.err);
}
o.depth=(DepthReading)o.depth.clone();
o.temperature=(TemperatureReading)o.temerature.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();
}
}
倒数第11.12行不知道为什么报错,o是一个OceanReading类来的啊,为什么不能调用depth跟temperature属性呢?
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(System.err);
}
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(System.err);
}
return o;
}
}
class OceanReading implements Cloneable{
private DepthReading depth;
private TemperatureReading temperature;
public OceanReading(double tdata,double ddata){
temperature=new TemperatureReading(tdata);
depth=new DepthReading(ddata);
}
public Object clone(){
Object o=null;
try{
o=(OceanReading)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace(System.err);
}
o.depth=(DepthReading)o.depth.clone();
o.temperature=(TemperatureReading)o.temerature.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();
}
}
倒数第11.12行不知道为什么报错,o是一个OceanReading类来的啊,为什么不能调用depth跟temperature属性呢?
Object o=null;
try
{
o=(OceanReading)super.clone();
}catch(CloneNotSupportedException e)
{
e.printStackTrace(System.err);
}
你看,你的o是Object类型的,怎么能调用OceanReading里的属性呢?
public Object clone()
{
OceanReading o = null;
try
{
o = (OceanReading) super.clone();
} catch (CloneNotSupportedException e)
{
e.printStackTrace(System.err);
}
o.depth = (DepthReading) o.depth.clone();
o.temperature = (TemperatureReading) o.temperature.clone();
return o;
}