java中怎么复制一个对象? 直接implements Cloneable然后clone(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我把原来一个C#的程序想用java改写,中间要复制一个image对象,C#中类似Image c = (Image)b.clone()就完成了现在在java中,我试着写了一个abstract class myImage extends Image implements Cloneable{ public Object clone() { Object o = null; try { o = super.clone(); } catch(CloneNotSupportedException cnse) { cnse.printStackTrace(); } return o; }}但createImage方法返回的是Image对象,myImage类就用不上了。sigh~,谁能帮一把? 你要明白这个问题还是好好去研究一下 clone 我稍微看了一点,分什么shallow clone和deep clone,似乎就是考虑到被克隆对象里包含的对象问题吧我现在只是简单得想把一个Image对象复制一下,应属于shallow clone吧,不知什么方法可以? o = super.clone();应该就是返回自身把。 如果你是shallow Clone那么,就return super.clone();就可以。如果要是深度Clone的话,那么就要自己做了myImage o = (myImage) super.clone();然后就自己做可以了:) 深度克隆和浅克隆的离职,我修改并测试了,利用了think in java 中的例子修改的,呵呵class DepthReading implements Cloneable { private double depth; public DepthReading(double depth) { this.depth = depth; } public void setDepth(double newDepth){ this.depth = newDepth; } public double getDepth(){ return this.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 void setTemperature(double newTemperature){ this.temperature = newTemperature; } public double getTemperature(){ return this.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; private java.util.GregorianCalendar gc; private String str; private Integer integer; private Boolean bool; public OceanReading(double tdata, double ddata){ temperature = new TemperatureReading(tdata); depth = new DepthReading(ddata); } public void setGC(java.util.GregorianCalendar newGC){ this.gc = newGC; } public java.util.GregorianCalendar getGC(){ return this.gc; } public void setInteger(Integer newInt){ this.integer = newInt; } public Integer getInteger(){ return this.integer; } public DepthReading getDepthReading (){ return depth; } public TemperatureReading getTemperatureReading(){ return temperature; } public Object clone() { OceanReading o = null; try { o = (OceanReading)super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } // Must clone handles: //o.depth = (DepthReading)o.depth.clone(); // o.temperature = (TemperatureReading)o.temperature.clone(); o.depth = this.getDepthReading(); o.temperature = this.getTemperatureReading(); return o; // Upcasts back to Object }}//-----------------------------------------------------------------------------------------------------public class DeepCopy { public static void main(String[] args) { OceanReading reading = new OceanReading(33.9, 100.5); reading.setInteger(new Integer(5)); reading.setGC(new java.util.GregorianCalendar()); // Now clone it: System.out.println(reading.getTemperatureReading().getTemperature()); System.out.println(reading.getInteger().toString()); System.out.println(reading.getGC().get(java.util.GregorianCalendar.YEAR)); OceanReading r = (OceanReading)reading.clone(); r.setInteger(new Integer(8)); r.getTemperatureReading().setTemperature(1.2); //r.setGC(new java.util.GregorianCalendar(2008,8,8)); r.setGC(reading.getGC()); r.getGC().set(java.util.GregorianCalendar.YEAR,2008); System.out.println(r.getTemperatureReading().getTemperature()); System.out.println(r.getInteger().toString()); System.out.println(r.getGC().get(java.util.GregorianCalendar.YEAR)); System.out.println(reading.getTemperatureReading().getTemperature()); System.out.println(reading.getInteger().toString()); System.out.println(reading.getGC().get(java.util.GregorianCalendar.YEAR)); }} 可以用myimage实现clone,可参数只能接受为image类型,怎么办? 解析JAVA中常见异常类型与原因 扫描条形码的原理的题目 求基于java语言的节气计算方法 关于list的两个访问方法的效率问题 java调用VC写的DLL,如何传递参数 请教一个问题(about JTable and JComboBox) Java 图形处理 关于在startup.bat中设置环境变量的问题 一个数据类型转换问题,向诸位请教! question on java 这类代码看了一点也不明白,应该看什么书呢? 好像很奇怪
现在在java中,我试着写了一个
abstract class myImage extends Image implements Cloneable{
public Object clone() {
Object o = null;
try {
o = super.clone();
}
catch(CloneNotSupportedException cnse) {
cnse.printStackTrace();
}
return o;
}
}
但createImage方法返回的是Image对象,myImage类就用不上了。
sigh~,谁能帮一把?
我现在只是简单得想把一个Image对象复制一下,应属于shallow clone吧,不知什么方法可以?
应该就是返回自身把。
就可以。如果要是深度Clone的话,那么就要自己做了
myImage o = (myImage) super.clone();
然后就自己做可以了:)
class DepthReading implements Cloneable {
private double depth;
public DepthReading(double depth) {
this.depth = depth;
}
public void setDepth(double newDepth){
this.depth = newDepth;
}
public double getDepth(){
return this.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 void setTemperature(double newTemperature){
this.temperature = newTemperature;
}
public double getTemperature(){
return this.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;
private java.util.GregorianCalendar gc;
private String str;
private Integer integer;
private Boolean bool;
public OceanReading(double tdata, double ddata){
temperature = new TemperatureReading(tdata);
depth = new DepthReading(ddata);
}
public void setGC(java.util.GregorianCalendar newGC){
this.gc = newGC;
}
public java.util.GregorianCalendar getGC(){
return this.gc;
}
public void setInteger(Integer newInt){
this.integer = newInt;
}
public Integer getInteger(){
return this.integer;
}
public DepthReading getDepthReading (){
return depth;
}
public TemperatureReading getTemperatureReading(){
return temperature;
}
public Object clone() {
OceanReading o = null;
try {
o = (OceanReading)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
// Must clone handles:
//o.depth = (DepthReading)o.depth.clone();
// o.temperature = (TemperatureReading)o.temperature.clone();
o.depth = this.getDepthReading();
o.temperature = this.getTemperatureReading(); return o; // Upcasts back to Object
}
}
//-----------------------------------------------------------------------------------------------------
public class DeepCopy {
public static void main(String[] args) {
OceanReading reading =
new OceanReading(33.9, 100.5);
reading.setInteger(new Integer(5));
reading.setGC(new java.util.GregorianCalendar());
// Now clone it:
System.out.println(reading.getTemperatureReading().getTemperature());
System.out.println(reading.getInteger().toString());
System.out.println(reading.getGC().get(java.util.GregorianCalendar.YEAR));
OceanReading r =
(OceanReading)reading.clone();
r.setInteger(new Integer(8));
r.getTemperatureReading().setTemperature(1.2);
//r.setGC(new java.util.GregorianCalendar(2008,8,8));
r.setGC(reading.getGC());
r.getGC().set(java.util.GregorianCalendar.YEAR,2008);
System.out.println(r.getTemperatureReading().getTemperature());
System.out.println(r.getInteger().toString());
System.out.println(r.getGC().get(java.util.GregorianCalendar.YEAR));
System.out.println(reading.getTemperatureReading().getTemperature());
System.out.println(reading.getInteger().toString());
System.out.println(reading.getGC().get(java.util.GregorianCalendar.YEAR)); }
}