if(c.color == this.color && c.height == this.height && c.weight == this.weight) 这里this指的是?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);
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/23 04:23:48
if(c.color == this.color && c.height == this.height && c.weight == this.weight) 这里this指的是?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);
if(c.color == this.color && c.height == this.height && c.weight == this.weight) 这里this指的是?
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;
}
}
if(c.color == this.color && c.height == this.height && c.weight == this.weight) 这里this指的是?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);
this 表示你要执行操作的类对象,比如你定义了Object a1;并进行初始化,现在a1.equal(Object boj),这个时候this就代表的是a1这个类对象的指针.可以调用a1中的成员