📄 testequalsmethod.java
字号:
package Operator;
/*
equals()的默认行为是比较句柄,除非在自己的新类中改变了equals(),
否则不可能表现出我们希望的行为。大多数Java类库都实现了equals(),
所以它实际比较的是对象的内容,而非它们的句柄。
*/
public class TestEqualsMethod {
public static void main(String[] args) {
//Integer类重写了equals()方法用以比较内容而非句柄
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println("Do n1 and n2 have the same content?"+n1.equals(n2));
//String类重写了equals()方法用以比较内容而非句柄
String s1 = "I am a student.";
String s2 = "I am a student.";
System.out.println("Do s1 and s2 have the same content?"+s1.equals(s2));
//Man类没有重写equals()方法,因此比较句柄而非内容
Man m1=new Man();
Man m2=new Man();
m1.age=36;
m2.age=36;
System.out.println("Do m1 and m2 have the same content?"+m1.equals(m2));
//Tree类重写了equals()方法用以比较内容而非句柄
Tree t1 = new Tree();
Tree t2 = new Tree();
System.out.println("Do t1 and t2 have the same content?"+t1.equals(t2));
}
}
class Man{
int age;
}
class Tree{
int age;
boolean equals(Tree t){
if(this.age==t.age)
return true;
else
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -