日期:2014-05-20  浏览次数:20644 次

求教一个小问题,看不懂......
不多说了,赶紧上代码,小弟菜鸟一枚,今天自己按照老师的代码照猫画虎,结果能够编译,但是没有结果输出,求大侠帮帮忙。
Java code

public class Cat {

    /**
     * @author 
     */
    private String color;
    private int height;
    private int weight;
    Cat(String color,int height,int weight){
        this.color=color;
        this.height=height;
        this.weight=weight;
    }
    public static String Deng(Cat a,Cat b){
        if(a==b){
            return "C1==C2";
        }else {
            return "C1!=C2";
        }
    }
    public static String EquMao(Cat c,Cat d){
        if(c.equals(d)){
            return "C1 Equels C2";
        }else{
            return "C1 is not Equels C2";
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Cat c1=new Cat("Blue",5,6);
        Cat c2=new Cat("Blue",5,6);
        Deng(c1, c2);
        EquMao(c1, c1);
    }
    
}


大侠帮帮忙,小弟感激不尽。

------解决方案--------------------
看来我答非所问了,没有输出结果是因为你么有打印。。。
Java code

public class Cat {
    /**
     * @author
     */
    private String color;
    private int height;
    private int weight;

    Cat(String color, int height, int weight) {
        this.color = color;
        this.height = height;
        this.weight = weight;
    }

    public static String Deng(Cat a, Cat b) {
        if (a == b) {
            return "C1==C2";
        } else {
            return "C1!=C2";
        }
    }

    public static String EquMao(Cat c, Cat d) {
        if (c.equals(d)) {
            return "C1 Equals C2";
        } else {
            return "C1 is not Equals C2";
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {//同一对象直接返回true
            return true;
        }
        if (obj instanceof Cat) {//是不是 Cat 的实例,如果是,强转类型
            Cat cat = (Cat) obj;
            if (cat.color.equals(this.color) && cat.height == this.height
                    && cat.weight == this.height) {//属性相同,则为equals
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Cat c1 = new Cat("Blue", 5, 6);
        Cat c2 = new Cat("Blue", 5, 6);
        System.out.println(Deng(c1, c2));
        System.out.println(EquMao(c1, c1));
    }
}