//Student.javaLàm thế nào để sử dụng phương thức hashCode gốc() sau khi trọng nó
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());
}
}
Như trong đoạn mã trên, tôi đã ghi đè phương thức hashCode()
. Điều này nghe có vẻ ngớ ngẩn, nhưng tôi có thể truy cập phương thức java.lang.Object.hashCode()
sử dụng cùng một đối tượng Sinh viên (s1 và s2) cùng một lúc không?
'super.hashCode() ', nhưng điều đó sẽ không sử dụng IMHO. –