免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Java 求解
最近访问板块 发新帖
查看: 3802 | 回复: 1

求解 [复制链接]

论坛徽章:
0
发表于 2014-07-12 09:01 |显示全部楼层
public class TestEquals {
       
        class Cat {
               
                int color ;
                int weight ;
                int height ;
               
                public Cat (int color ,int weight ,int height) {
                       
                        this.color = color ;
                        this.weight = weight ;
                        this.height = height ;
                }
        }
       
        public boolean equals(Object obj) {
               
                if (obj == null) {
                        return false ;
                }
               
                else {
                        if (obj instanceof Cat) {
                                Cat c = (Cat) obj ;
                               
                                if(c.color == this.color && c.weight == this.weight && c.height == this.height){
                                        return true ;
                                }
                               
                                else {
                                        return false ;
                                }
                        }
                }
        }
       
       
        public static void main (String[] args) {
               
                Cat c1 = new Cat (1 ,2 ,3);
                Cat c2 = new Cat (1 ,2 ,3);
               
                //System.out.println (c1 == c2);
                System.out.println (c1.equals (c2) );
        }
}
360截图20140712085214828.jpg

论坛徽章:
39
白银圣斗士
日期:2015-11-24 10:40:40酉鸡
日期:2015-03-20 14:15:44寅虎
日期:2015-03-20 14:13:59午马
日期:2015-03-20 14:13:16白羊座
日期:2015-03-20 14:12:54金牛座
日期:2015-03-20 14:12:09双子座
日期:2015-03-20 14:11:57巨蟹座
日期:2015-03-20 14:11:44狮子座
日期:2015-03-20 14:11:29亥猪
日期:2015-03-20 14:16:24戌狗
日期:2015-03-20 14:16:40申猴
日期:2015-03-20 14:17:05
发表于 2014-07-14 10:05 |显示全部楼层
你的equals是Cat的,不是TestEquals的,你放错地方了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP