Would you like to react to this message? Create an account in a few clicks or log in to continue.

ҷӡ override equals() ҵͧӡ override hashCode()

Go down

ҷӡ override equals()  ҵͧӡ override hashCode()  Empty ҷӡ override equals() ҵͧӡ override hashCode()

ตั้งหัวข้อ  Admin Wed May 21, 2008 3:16 pm

ҷӡ override equals() ҵͧӡ override hashCode()

The Java super class java.lang.Object has two very important methods defined in it. They are -

  • public boolean equals(Object obj)
  • public int hashCode()

ҧ
1. public class Test
2. {
3. private int num;
4. private String data;
5.
6. public boolean equals(Object obj)
7. {
8. if(this == obj)
9. return true;
10. if((obj == null) || (obj.getClass() != this.getClass()))
11. return false;
12. // object must be Test at this point
13. Test test = (Test)obj;
14. return num == test.num &&
15. (data == test.data || (data != null && data.equals(test.data)));
16. }
17.
18. public int hashCode()
19. {
20. int hash = 7;
21. hash = 31 * hash + num;
22. hash = 31 * hash + (null == data ? 0 : data.hashCode());
23. return hash;
24. }
25.
26. // other methods
27. }
Admin
Admin
Admin

จำนวนข้อความ : 112
Registration date : 18/05/2008

http://chawban.chocoforum.com

ขึ้นไปข้างบน Go down

ขึ้นไปข้างบน

- Similar topics

 
Permissions in this forum:
คุณไม่สามารถพิมพ์ตอบ