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

teaky2002 teaky2002 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位帅哥靓妹,本人有一个问题始终想不通,如下程序:

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接口,应该是一样的啊!请不吝赐教。绝对给分。
...全文
评论 4
收藏 已收藏
分享
返回顶部
teaky2002
Java SE 62,621 成员 307,251 内容