IWILLCOMINGBACK 发表于 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) );
        }
}

rover12421 发表于 2014-07-14 10:05

你的equals是Cat的,不是TestEquals的,你放错地方了
页: [1]
查看完整版本: 求解