public class TestEquals {
public static void mian(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));
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}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.height){
return true;
}
}
}
return false;
}
}
public static void mian(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));
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}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.height){
return true;
}
}
}
return false;
}
}
解决方案 »
- 关于在txt文本中的字符串的替换问题
- java怎么读取doc和rtf格式的内容到txt文件中???
- 那位朋友给我个思路,java获取多个非功能键同时按下
- 请问浮点数输出的问题
- 在 DOS 和editplus 下可以运行成功,可是在 JCreator 下却有错误如下:
- 请问如何把从pl/sql中读出的OracleTypes.DATE型数据转换成String型“yyyy/MM/dd HH:mm”
- 看看这个运行后会产生什么
- +++++无运算结果+++++
- 请问哪里有jre1.1.8下载,谢谢。
- 请问 JMS 是什么技术?
- 希望大家谈谈,在类的内部,变量定义的先后顺序决定了初始化的先后顺序是怎么回事?
- 有关帮助的事???
public static void main(String[] args){
public static void mian仔细些,你写成“mian”了。
c.color == this.color && c.height == this.height && c.weight == this.height判断条件也不对
public static void main(String[] args){ //这行你原来是mian.
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));
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
} 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;
} }