public class UserWord extends WordADT {
public int WORD_STATUS;
public int POINT_OF_WORD;
public int COUNT_OF_WRONG_ANSWER;
@Override
public Object getClone() throws CloneNotSupportedException {
return super.clone();
}
}
AND `
Userword temp = new Userword();
Usertword temp2 = temp.getClone(); //this way doesn't work.
I can't use getClone() method. I'm getting this error. How can i clone a instance?
java.lang.CloneNotSupportedException: Class UserWord doesn't implement Cloneable.
Fixed: clone:() method needs to implement IClonable inferface
答案 0 :(得分:0)
使用它来克隆任何对象:
public static Object deepClone(Object object) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
ByteArrayInputStream bais = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
return null;
}
}
在你的案例中使用如下:
Usertword temp2 = (Usertword)deepClone(temp);