| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 570 人关注过本帖
标题:求解--如何才能编译成功?
只看楼主 加入收藏
水浙江雪
Rank: 1
等 级:新手上路
帖 子:104
专家分:3
注 册:2014-5-17
结帖率:95.83%
收藏
已结贴  问题点数:10 回复次数:4 
求解--如何才能编译成功?
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) );
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: Object public return equals false 
2014-07-12 08:53
funyh250
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:26
帖 子:290
专家分:1573
注 册:2013-12-25
收藏
得分:10 
第一类错误:
  这里不能想当然的用this 用个其他方法替代吧

第二类错误:
  改法1:  1.先创建一个对象 2.再用赋值语句赋值
 改法2:    color = ncolor ;
            weight = nweight ;
            height = nheight ;注意参数和数据成员的名称最好不一样
      这样你的new语句应该没问题了

学习是大事   吃喝拉撒睡是小事   其他的那都不是事
2014-07-12 10:15
水浙江雪
Rank: 1
等 级:新手上路
帖 子:104
专家分:3
注 册:2014-5-17
收藏
得分:0 
回复 2 楼 funyh250 我把 this 和变量名改了,可是还是这样?
public class TestEquals2 {
   
    class Cat {
        
        int color ;
        int weight ;
        int height ;
        
        public Cat (int _color ,int _weight ,int _height) {
            
            color = _color ;
            weight = _weight ;
            height = _height ;
        }
    }
   
    public boolean equals(Object obj) {
        
        if (obj == null) {
            return false ;
        }
        
        else {
            if (obj instanceof Cat) {
                Cat c = (Cat) obj ;
               
                if(c.color == _color && c.weight == _weight && c.height == _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) );
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册

我把this和变量名改了,可是还是这样?

打败别人,只是人生的赢家;打败自己,才是命运的强者
2014-07-12 10:43
水浙江雪
Rank: 1
等 级:新手上路
帖 子:104
专家分:3
注 册:2014-5-17
收藏
得分:0 
回复 2 楼 funyh250
我知道错在哪里了,我定义的Cat类是内部类,要建立内部类对象才可以访问到。
或者把内部类提出来就可以了。

打败别人,只是人生的赢家;打败自己,才是命运的强者
2014-07-12 11:14
水浙江雪
Rank: 1
等 级:新手上路
帖 子:104
专家分:3
注 册:2014-5-17
收藏
得分:0 
回复 2 楼 funyh250 这是我修改后的代码
这是我修改后的代码,OK了。

public class TestEqualsKey {
     
    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) );
    }
}


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 ;
                }
            }
        }
        
        return false ;
    }
}
   
   
图片附件: 游客没有浏览图片的权限,请 登录注册

打败别人,只是人生的赢家;打败自己,才是命运的强者
2014-07-12 11:17
快速回复:求解--如何才能编译成功?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015834 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved