关于对象clone()疑问,为什么要在子类中覆写该方法?

yw1530 2011-06-22 10:46:41

class Person implements Cloneable{ // 实现Cloneable接口表示可以被克隆
private String name ;
public Person(String name){
this.name = name ;
}
public void setName(String name){
this.name = name ;
}
public String getName(){
return this.name ;
}
public Object clone() throws CloneNotSupportedException{ //覆写Object clone()方法
return super.clone();
}
};

public class CloneDemo01 {
public static void main(String args[]) throws Exception{
Person p1 = new Person("张三") ;
Person p2 = null ;
try{
p2 = (Person)p1.clone();
}
catch(Exception e){
}
}
};

以上代码中,Person实现了Cloneable接口,并覆写了Object类的clone()方法后,在CloneDemo01类中就可以调用p2 = (Person)p1.clone();实现克隆,但有个疑问:
如果不重写clone()方法,调用p2 = (Person)p1.clone();就会出错。
所有类默认都是继承Object类,Object类的clone()方法为protected修饰,该修饰不是可以直接由子类直接访问吗?
CloneDemo01也是Object的子类,为什么还要在Person中覆写才能使用p1.clone();呢?请教高手!
...全文
200 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqlwq123 2011-06-23
  • 打赏
  • 举报
回复
LZ你把类和对象混一起了
zhouYunan2010 2011-06-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cai5 的回复:]

引用 3 楼 alexandertech 的回复:
protected native Object clone() throws CloneNotSupportedException;

protected方法,在子类中可以调用,但
p2 = (Person)p1.clone();
这是在外部运行了,protected方法不支持外部调用

+1
[/Quote]
+1
这是为了防止clone方法被随便调用
liuzuowei007 2011-06-22
  • 打赏
  • 举报
回复

/**
* A class implements the <code>Cloneable</code> interface to
* indicate to the {@link java.lang.Object#clone()} method that it
* is legal for that method to make a
* field-for-field copy of instances of that class.
* <p>
* Invoking Object's clone method on an instance that does not implement the
* <code>Cloneable</code> interface results in the exception
* <code>CloneNotSupportedException</code> being thrown.
* <p>
* By convention, classes that implement this interface should override
* <tt>Object.clone</tt> (which is protected) with a public method.
* See {@link java.lang.Object#clone()} for details on overriding this
* method.
* <p>
* Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
* Therefore, it is not possible to clone an object merely by virtue of the
* fact that it implements this interface. Even if the clone method is invoked
* reflectively, there is no guarantee that it will succeed.
*
* @author unascribed
* @version 1.17, 11/17/05
* @see java.lang.CloneNotSupportedException
* @see java.lang.Object#clone()
* @since JDK1.0
*/
public interface Cloneable {
}




看下注释便知究竟
五哥 2011-06-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 alexandertech 的回复:]
protected native Object clone() throws CloneNotSupportedException;

protected方法,在子类中可以调用,但
p2 = (Person)p1.clone();
这是在外部运行了,protected方法不支持外部调用
[/Quote]
+1
飞跃颠峰 2011-06-22
  • 打赏
  • 举报
回复

protected native Object clone() throws CloneNotSupportedException;

protected方法,在子类中可以调用,但
p2 = (Person)p1.clone();
这是在外部运行了,protected方法不支持外部调用
皮特张 2011-06-22
  • 打赏
  • 举报
回复
JDK里面是这样写的:

Object 类的 clone 方法执行特定的克隆操作。首先,如果此对象的类不能实现接口 Cloneable,则会抛出 CloneNotSupportedException。注意:所有的数组都被视为实现接口 Cloneable。否则,此方法会创建此对象的类的一个新实例,并像通过分配那样,严格使用此对象相应字段的内容初始化该对象的所有字段;这些字段的内容没有被自我克隆。所以,此方法执行的是该对象的“浅表复制”,而不“深层复制”操作。

Object 类本身不实现接口 Cloneable,所以在类为 Object 的对象上调用 clone 方法将会导致在运行时抛出异常。
yw1530 2011-06-22
  • 打赏
  • 举报
回复
求高手啊!

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧