//Student.java如何使用原始的hashCode()方法覆蓋其
class Student{
private int roll;
private String name;
public Student(int roll,String name){
this.roll=roll;
this.name=name;
}
public int hashCode(){
return roll+name.length();
}
public boolean equals(Object obj){
Student s=(Student)obj;
return (this.roll==s.roll && this.name.equals(s.name));
}
}
//IssueID.java
class IssueID{
public static void issueID(Student s1,Student s2){
if(s1.equals(s2))
System.out.println("New ID issued");
else
System.out.println("New ID NOT issued");
}
}
//Institute.java
import java.lang.Object;
class Institute{
public static void main(String[] args){
Student s1=new Student(38,"shiva");
Student s2=new Student(45,"aditya");
IssueID.issueID(s1,s2);
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
}
}
後正如在上面的代碼中,我已經覆蓋了hashCode()
方法。這聽起來很愚蠢,但我可以同時使用相同的Student對象(s1和s2)訪問java.lang.Object.hashCode()
方法嗎?
'super.hashCode(',但是這將是沒有用的恕我直言。 –