Topic: Java什么时候会抛出CloneNotSupportedException? |
Print this page |
1.Java什么时候会抛出CloneNotSupportedException? | Copy to clipboard |
Posted by: virmin Posted on: 2006-07-21 17:21 Object有clone(),为protected,所以所有子类都会有这个clone(),而且,都可以访问到这个方法,看书上在某个自定义的类的clone()里,总用try把super.clone()给罩住,catch这个CloneNotSupportedException,但是实际会抛出这个异常吗?那是在什么样的情况下,会有这样的异常发生?哪位告诉我?Please! |
2.Re:Java什么时候会抛出CloneNotSupportedException? [Re: virmin] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2006-07-22 21:56 Here is the code of Object clone method.
You need to override it in your class to use it. |
3.Re:Java什么时候会抛出CloneNotSupportedException? [Re: virmin] | Copy to clipboard |
Posted by: jigsaw Posted on: 2006-07-23 02:33 楼上的同学先看看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. 然后写个例子看看:
运行下看看结果如何 然后改一点点看:
这次又怎样? |
4.Re:Java什么时候会抛出CloneNotSupportedException? [Re: virmin] | Copy to clipboard |
Posted by: jigsaw Posted on: 2006-07-23 02:34 而什么时候要try-catch,这个问题你google一下看 |
5.Re:Java什么时候会抛出CloneNotSupportedException? [Re: virmin] | Copy to clipboard |
Posted by: virmin Posted on: 2006-07-24 10:46 感谢回答,我想是我没说清楚,我是问什么时候会抛出,因为从Object而来的子类,在实现clone()时,总是能调用super.clone(),而Object的clone()会抛出CloneNotSupportedException吗?没有,只是声明了会,但实际上没有抛出。那么子类中try{}catch{}干什么,根本不会有这个CloneNotSupportedException啊 |
6.Re:Java什么时候会抛出CloneNotSupportedException? [Re: virmin] | Copy to clipboard |
Posted by: virmin Posted on: 2006-07-24 10:54 试了下jigsaw的例子,有点明白了 我的理解,不知道是否对,请指教 Object声明的protected clone虽然可以阻止没实现clone()的子类,在子类外部直接调用clone(),但是对静态方法却没有办法,因为静态方法可以直接访问protected的方法,而这样就有可能会出现对对象使用clone()而实际对象不支持clone()。 第一,二两个例子里的Test,它们都没有实现clone()函数,所以m.clone()调用的是Object的clone(),而Object的clone()通过RTTI来检查对象是否支持Cloneable接口,支持的,按位复制,不支持的,抛出CloneNotSupportedException。 Am I right? |
7.Re:Java什么时候会抛出CloneNotSupportedException? [Re: virmin] | Copy to clipboard |
Posted by: jigsaw Posted on: 2006-07-25 09:56 你的理解是错的 关于clone 请上网google一下 关于这方面 很多文章说过的 |
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 |