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

问一个复杂的return问题

public class return2 {
public static void main(String[] args) 

Cat c1 = new Cat(1,2,3); //实例化
Cat c2 = new Cat(1,2,3); 
System.out.println(c1.equals(c2)); 


class Cat { 
int colour; 
int height,weight; 
public Cat(int colour,int height,int weight) { 
this.colour = colour; //使用全局变量
this.height = height; //使用全局变量
this.weight = weight; //使用全局变量

public boolean equals(Object obj) { 
if (obj==null) return false; 
else 
if(obj instanceof Cat) { 
Cat c = (Cat)obj; 
if((c.colour==colour)&&(c.height==height)&&(c.weight==weight)) { 
return true; 


return false; 


}

这个句子应该怎么理解啊,请能够详细点说明一下步骤,谢谢。

------解决方案--------------------
判断两个Cat类对象相等,两个都是Cat实例,而且对象的3个属性相等即为两个对象相等。
------解决方案--------------------
false
------解决方案--------------------
你重写了该对象的equals方法,只需要他的三个属性值相等就OK了,所以不存在回去比较内存位置之类的。
------解决方案--------------------
重写了equals方法。
并且需要满足重写equals的几个要求,所以才有那些判断