public class TestEquals {
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 height, weight;
public Cat(int color, int height, int weight) {
this.color = color;
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.color == this.color && c.height = this.height && c.weight == this.weight) {
return true;
}
}
}
return false;
}
} 这个程序为什么编译的时候说
TestEquals.java:28: 运算符 && 不能应用于 boolean,int
if(c.color == this.color && c.height = this.heig
ht && c.weight == this.weight) {
^
TestEquals.java:28: 运算符 && 不能应用于 int,boolean
if(c.color == this.color && c.height = this.heig
ht && c.weight == this.weight) { ^
请问怎么回事? 按照我的理解,这个错误是这个原因:先比较这个c.color == this.color ,得出一个boolean值,然后&&c.height比较,所以提示运算符 && 不能应用于 int,boolean,可是运算符有优先性啊?
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 height, weight;
public Cat(int color, int height, int weight) {
this.color = color;
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.color == this.color && c.height = this.height && c.weight == this.weight) {
return true;
}
}
}
return false;
}
} 这个程序为什么编译的时候说
TestEquals.java:28: 运算符 && 不能应用于 boolean,int
if(c.color == this.color && c.height = this.heig
ht && c.weight == this.weight) {
^
TestEquals.java:28: 运算符 && 不能应用于 int,boolean
if(c.color == this.color && c.height = this.heig
ht && c.weight == this.weight) { ^
请问怎么回事? 按照我的理解,这个错误是这个原因:先比较这个c.color == this.color ,得出一个boolean值,然后&&c.height比较,所以提示运算符 && 不能应用于 int,boolean,可是运算符有优先性啊?
return true;
}
c.height = this.height
少了个等号 - -