Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 有关克隆的问题
xingchao





发贴: 32
积分: 0
于 2006-11-15 16:57 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
请大家帮帮忙解决个问题
package questions.c2;
public class Debug2_2 implements Cloneable{
  StringBuffer sb=new StringBuffer("Salse Report for");
  public Debug2_2 clone()
   throws CloneNotSupportedException{
     Debug2_2 result=super.clone();
     result.sb=new StringBuffer(this.sb.toString());
     return result;
   }
  
   public static void main(String[] args)
   throws CloneNotSupportedException{
   Debug2_2 x=new Debug2_2();
   Debug2_2 y=x.clone();
   x.sb.append("October");
   System.out.println(y.sb);
   }
}
这个程序中 在编译时 报错 “不兼容的类型” 第六行
这是怎么回事啊? 希望大家多多指点!



作者 Re:有关克隆的问题 [Re:xingchao]
stonefeng





发贴: 4
积分: 0
于 2006-12-11 22:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
super.clone()的返回值类型为Object,而你要将其赋值给Debug2_2类型,显然类型不兼容。


作者 Re:有关克隆的问题 [Re:xingchao]
focus





发贴: 42
积分: 2
于 2006-12-12 16:24 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Debug2_2 result=super.clone();
俺笨,不知道这里的 super 指的是谁?



作者 Re:有关克隆的问题 [Re:xingchao]
cxp108





发贴: 363
积分: 12
于 2006-12-13 08:42 user profilesend a private message to usersend email to cxp108search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Java中所有的类都 extends 自Object,因此不管你使用没使用extends关键字,只要是class就一定继承自Object


作者 Re:有关克隆的问题 [Re:xingchao]
stonefeng





发贴: 4
积分: 0
于 2006-12-13 21:36 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
从你的类定义
  public class Debug2_2 implements Cloneable
来看,Debug2_2直接从Object类继承,因而super应该是Object.

而cxp108的说法也不完全:
确切的说法应该是上一级父类,只有当上一级父类没有找到clone()方法时,则向再上一级父类寻找该方法,一直到最高级父类Object为止。




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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