Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 实战错误讨论  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Object类中最基本又最深层的问题
swofmengtian





发贴: 2
积分: 0
于 2005-11-10 16:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
package mypack;
public class MyObject {

  public static void main(String[] args) {
    Object me=new Object();
    me.clone();
    MyObject my=new MyObject();
    my.clone();
  }
}



作者 Re:Object类中最基本又最深层的问题 [Re:swofmengtian]
swofmengtian





发贴: 2
积分: 0
于 2005-11-10 16:26 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
两个clone()方法都用不了,根本就编译不过,为什么?


作者 Re:Object类中最基本又最深层的问题 [Re:swofmengtian]
wangjiong





发贴: 17
积分: 17
于 2005-11-10 17:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
看你的编译错误信息不就知道了。object 的clone方法是protected,当然不能直接调用。第二个你没有捕捉抛出的异常。


作者 Re:Object类中最基本又最畈愕奈侍?></td></tr><tr class= [Re:swofmengtian]
ranchgirl



版主


发贴: 801
积分: 132
于 2005-11-11 14:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Q. How to use clone()method, why my code throws CloneNotSupportedException?
A: Make your class implements Cloneable interface, and override the Object.clone() method.

If you don't override the Object.clone() method, the code will be compilable, but throws CloneNotSupportedException. Why, see the following code copied from JDK Object.java.

protected native Object clone() throws CloneNotSupportedException;



作者 Re:Object类中最基本又最深层的问题 [Re:swofmengtian]
ranchgirl



版主


发贴: 801
积分: 132
于 2005-11-11 15:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Read javadoc here:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#clone()



作者 Re:Object类中最基本又最深层的问题 [Re:swofmengtian]
wallacer

HUST



发贴: 30
积分: 1
于 2005-11-11 20:43 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Advice:
To lean java clone method you must carefully understand shadow clone and deep clone. Here are the examples:

shadow clone:

class UnCloneA {
private int i;
public UnCloneA(int ii) { i = ii; }
public void doubleValue() { i *= 2; }
public String toString() {
return Integer.toString(i);
}
}
class CloneB implements Cloneable{
public int aInt;
public UnCloneA unCA = new UnCloneA(111);
public Object clone(){
CloneB o = null;
try{
o = (CloneB)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return o;
}
}
public class CloneMain {
public static void main(String[] a){
CloneB b1 = new CloneB();
b1.aInt = 11;
System.out.println("before clone,b1.aInt = "+ b1.aInt);
System.out.println("before clone,b1.unCA = "+ b1.unCA);

CloneB b2 = (CloneB)b1.clone();
b2.aInt = 22;
b2.unCA.doubleValue();
System.out.println("=========================");
System.out.println("after clone,b1.aInt = "+ b1.aInt);
System.out.println("after clone,b1.unCA = "+ b1.unCA);
System.out.println("=========================");
System.out.println("after clone,b2.aInt = "+ b2.aInt);
System.out.println("after clone,b2.unCA = "+ b2.unCA);
}
}


RESULT:
before clone,b1.aInt = 11
before clone,b1.unCA = 111
=================================
after clone,b1.aInt = 11
after clone,b1.unCA = 222
=================================
after clone,b2.aInt = 22
after clone,b2.unCA = 222

deep clone:

class UnCloneA implements Cloneable{
private int i;
public UnCloneA(int ii) { i = ii; }
public void doubleValue() { i *= 2; }
public String toString() {
return Integer.toString(i);
}
public Object clone(){
UnCloneA o = null;
try{
o = (UnCloneA)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return o;
}
}
class CloneB implements Cloneable{
public int aInt;
public UnCloneA unCA = new UnCloneA(111);
public Object clone(){
CloneB o = null;
try{
o = (CloneB)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
o.unCA = (UnCloneA)unCA.clone();
return o;
}
}
public class CloneMain {
public static void main(String[] a){
CloneB b1 = new CloneB();
b1.aInt = 11;
System.out.println("before clone,b1.aInt = "+ b1.aInt);
System.out.println("before clone,b1.unCA = "+ b1.unCA);

CloneB b2 = (CloneB)b1.clone();
b2.aInt = 22;
b2.unCA.doubleValue();
System.out.println("===========================");
System.out.println("after clone,b1.aInt = "+ b1.aInt);
System.out.println("after clone,b1.unCA = "+ b1.unCA);
System.out.println("===========================");
System.out.println("after clone,b2.aInt = "+ b2.aInt);
System.out.println("after clone,b2.unCA = "+ b2.unCA);
}
}


RUN RESULT:
before clone,b1.aInt = 11
before clone,b1.unCA = 111
=================================
after clone,b1.aInt = 11
after clone,b1.unCA = 111
=================================
after clone,b2.aInt = 22
after clone,b2.unCA = 222

There are the principles you must follow:
1. All basic types in java such as int, float, double, and so on of your class field can be cloned automatically.
2. All object types of your class field must be implemented the interface Cloneable, otherwise all the object types in your class field have been cloned point to the original class of its respective class field.


wallacer edited on 2005-11-11 21:03

 "your future depends on your dream, so go to sleep."
作者 Re:Object类中最基本又最深层的问题 [Re:swofmengtian]
ftang



版主


发贴: 214
积分: 38
于 2005-11-18 05:18 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Oh Boy! Clone() again...agree with wallacer: You MUST understand what is "Shadow Clone" and "Deep Clone" ----which unfortunately you can blame this on SUN, the Clone( ) design is a failure!!!!
The typical example is HashMap.clone(), which is shadow clone...so if the original hashMap value changed, then the copy hashMap value get changed( you still call this is a clone()?)

ps: Object类中最基本又最深层的问题? yeah...maybe to you it is 最深层....




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923