Image对象怎样实现克隆?

wanzhong 2001-05-23 01:07:00
谢谢!
...全文
58 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxyxl 2001-05-23
  • 打赏
  • 举报
回复
Clone是“克隆”的意思,即制作完全一模一样的副本,这个方法在基础类Object中定义成“protected”(受保护)模式。但在希望克隆的任何衍生类中,必须将其覆盖为“public”模式。所以你要想在Image对象中实现克隆,就必须自己重写Image类,让新类从Image继承过来,重写一下基础类的Clone方法,就可以了。例如,标准库类Vector覆盖了clone(),所以能为Vector调用clone(),他的部分源代码如下:
public class Vector extends AbstractList implements List, Cloneable,
java.io.Serializable
{
...............

/**
* Returns a clone of this vector. The copy will contain a
* reference to a clone of the internal data array, not a reference
* to the original internal data array of this <tt>Vector</tt> object.
*
* @return a clone of this vector.
*/
public synchronized Object clone() {
try {
Vector v = (Vector)super.clone();
v.elementData = new Object[elementCount];
System.arraycopy(elementData, 0, v.elementData, 0, elementCount);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}

....................
}

62,614

社区成员

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

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