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

您没有登录

» Java开发网 » Java SE 综合讨论区 » 学习心得/方法/资源  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Java什么时候会抛出CloneNotSupportedException?
virmin





发贴: 7
积分: 0
于 2006-07-21 17:21 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,所以所有子类都会有这个clone(),而且,都可以访问到这个方法,看书上在某个自定义的类的clone()里,总用try把super.clone()给罩住,catch这个CloneNotSupportedException,但是实际会抛出这个异常吗?那是在什么样的情况下,会有这样的异常发生?哪位告诉我?Please!


作者 Re:Java什么时候会抛出CloneNotSupportedException? [Re:virmin]
ranchgirl



版主


发贴: 801
积分: 132
于 2006-07-22 21:56 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
Here is the code of Object clone method.


protected native Object clone() throws CloneNotSupportedException;


You need to override it in your class to use it.





作者 Re:Java什么时候会抛出CloneNotSupportedException? [Re:virmin]
jigsaw

KK

CJSDN高级会员


发贴: 804
积分: 93
于 2006-07-23 02:33 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
楼上的同学先看看API怎么说 别教错了

public class CloneNotSupportedException
extends Exception
Thrown to indicate that the clone method in class Object has been called to clone an object, but that the object's class does not implement the Cloneable interface.

Applications that override the clone method can also throw this exception to indicate that an object could not or should not be cloned.

然后写个例子看看:



public class Test {  

  public static void main(String[] args) throws Exception {
    Test t = new Test();
    Test t2 = null;
    
    t2 = (Test) t.clone();
    System.out.println(t2);
  }

}



运行下看看结果如何

然后改一点点看:


public class Test implements Cloneable {  

  public static void main(String[] args) throws Exception {
    Test t = new Test();
    Test t2 = null;
    
    t2 = (Test) t.clone();
    System.out.println(t2);
  }

}



这次又怎样?


jigsaw edited on 2006-07-24 13:38

No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999
作者 Re:Java什么时候会抛出CloneNotSupportedException? [Re:virmin]
jigsaw

KK

CJSDN高级会员


发贴: 804
积分: 93
于 2006-07-23 02:34 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
而什么时候要try-catch,这个问题你google一下看


No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999
作者 Re:Java什么时候会抛出CloneNotSupportedException? [Re:virmin]
virmin





发贴: 7
积分: 0
于 2006-07-24 10: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
感谢回答,我想是我没说清楚,我是问什么时候会抛出,因为从Object而来的子类,在实现clone()时,总是能调用super.clone(),而Object的clone()会抛出CloneNotSupportedException吗?没有,只是声明了会,但实际上没有抛出。那么子类中try{}catch{}干什么,根本不会有这个CloneNotSupportedException啊


作者 Re:Java什么时候会抛出CloneNotSupportedException? [Re:virmin]
virmin





发贴: 7
积分: 0
于 2006-07-24 10:54 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
试了下jigsaw的例子,有点明白了

我的理解,不知道是否对,请指教

Object声明的protected clone虽然可以阻止没实现clone()的子类,在子类外部直接调用clone(),但是对静态方法却没有办法,因为静态方法可以直接访问protected的方法,而这样就有可能会出现对对象使用clone()而实际对象不支持clone()。

第一,二两个例子里的Test,它们都没有实现clone()函数,所以m.clone()调用的是Object的clone(),而Object的clone()通过RTTI来检查对象是否支持Cloneable接口,支持的,按位复制,不支持的,抛出CloneNotSupportedException。

Am I right?


virmin edited on 2006-07-24 11:07

作者 Re:Java什么时候会抛出CloneNotSupportedException? [Re:virmin]
jigsaw

KK

CJSDN高级会员


发贴: 804
积分: 93
于 2006-07-25 09:56 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 请上网google一下 关于这方面 很多文章说过的



No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999

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-2018 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号
客服电话 0592-8750026    客服信箱 714923@qq.com    客服QQ 714923