- public class HashCode {
- public String name = new String();
- public long age;
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!name.equals(((HashCode) obj).name))
- return false;
- if (age != ((HashCode) obj).age)
- return false;
- return true;
- }
- @Override
- public int hashCode() {
- return 1;
- }
- public static void main(String[] args) {
- HashCode his0 = new HashCode();
- HashCode his1 = new HashCode();
- his1.age = 1000;
- System.out.println(his0.equals(his1));
- HashCode2 hs2 = new HashCode2();
- }
- }
- class HashCode2 {
- HashCode element;
- String name2;
- long age2;
- @Override
- public boolean equals(Object obj) {
- if (element.hashCode() == obj.hashCode())
- return false;
- return super.equals(obj);
- }
- @Override
- public int hashCode() {
- return super.hashCode();
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2807201513245.html
来源: http://www.codesnippet.cn/detail/2807201513245.html