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

您没有登录

» Java开发网 » Java SE 综合讨论区 » Java与OOP初步  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:how to override the method equals() in myself's class [Re:roastduck]
tomcatexpert





发贴: 52
积分: 2
于 2006-04-11 12:19 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
你不需要比较所有的Member variables,根据你写的Class具体需要就可以了.

roastduck wrote:
instanceof is an operator
判断左边的object是否是右边class的instance

object instanceof class Smile 明白了.

next point

在这个例子中,如果class value 有多个field 例如:

class Value
{
int i;
char c;
//... may be more and more
}

那么在

  public boolean equals(Object that)
  {
    if (this == that)
    return true;
    if ( !(that instanceof Value) )
      return false;
    Value thatValue = (Value) that;
    //return (this.i == thatValue.i); //这里就要作如下修改,right?
return ((this.i == thatValue.i)&&(this.c == thatValue.c));
  }

是否有更简洁的写法? 毕竟field如果很多的话...最后那个return写起来会很难看的.




话题树型展开
人气 标题 作者 字数 发贴时间
10180 how to override the method equals() in myself's class roastduck 537 2006-04-09 20:44
8272 Re:how to override the method equals() in myself's class why 247 2006-04-09 22:13
8585 Re:how to override the method equals() in myself's class roastduck 392 2006-04-10 08:55
8231 Re:how to override the method equals() in myself's class why 142 2006-04-10 11:18
8719 Re:how to override the method equals() in myself's class roastduck 318 2006-04-10 00:11
8336 Re:how to override the method equals() in myself's class why 442 2006-04-10 00:34
8726 Re:how to override the method equals() in myself's class roastduck 772 2006-04-10 14:12
8845 Re:how to override the method equals() in myself's class tomcatexpert 865 2006-04-11 12:19
8458 Re:how to override the method equals() in myself's class java8964 3387 2006-04-11 03:56
8262 Re:how to override the method equals() in myself's class why 373 2006-04-11 06:16
8977 Re:how to override the method equals() in myself's class roastduck 1346 2006-04-11 13:08

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