既然是实现的Cloneable接口,应该是一样的啊!请不吝赐教。绝对给分。

teaky2002 2002-02-05 05:34:08
各位帅哥靓妹,本人有一个问题始终想不通,如下程序:

public class testclone implements Cloneable{
int a;
double b;
public testclone() {
}
public Object clone(){
try{
return super.clone();
}catch(CloneNotSupportedException e){
System.out.println("cloning not allowed.");
return this;
}
}
}


public class clonedemo {

public clonedemo() {
}
public static void main(String[] args) {
testclone x1=new testclone();
testclone x2;
x1.a=10;
x1.b=20.98;
x2=(testclone)x1.clone();
System.out.println("x1:"+x1.a+" "+x1.b);
System.out.println("x2:"+x2.a+" "+x2.b);
System.out.println(x1.equals(x2));
}
}


为什么最后System.out.println(x1.equals(x2))输出的结果是false;既然是实现的Cloneable接口,应该是一样的啊!请不吝赐教。绝对给分。
...全文
66 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbc218888 2002-02-05
  • 打赏
  • 举报
回复
upupupupupupupuppuupupupupupupup
nothing 2002-02-05
  • 打赏
  • 举报
回复
这年头,抢两分真不容易啊。仅仅掉线3分钟就有人抢先了。

bacchusboy2000(希腊酒神)兄说的很全了。

不过要注意,这段E文是说的Object类,对于其他(像String)类一般都实现了自己的equals方法。
dgzdx 2002-02-05
  • 打赏
  • 举报
回复
关注!
bacchusboy2000 2002-02-05
  • 打赏
  • 举报
回复
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).

所以,在实际中,如果想判断两对象的值是否相等,equals方法需要被重载的。

62,612

社区成员

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

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