Object
1 class Person{ 2 private String name ; 3 private int age; 4 public Person(String name,int age) { 5 this.name = name; 6 this.age = age; 7 } 8 // public int hashCode() { 9 // return super.hashCode(); 10 // } 11 public int hashCode() { 12 return this.age; 13 } 14 public String toString() { 15 return "姓名"+this.name+","+"年龄"+this.age; 16 } 17 //多态 子类对象赋值给父类变量 18 public boolean equals(Object obj) {//Object obj = new Person("王强",12); 19 Person p = (Person)obj;//向下强转 父类变量 obj 赋值给 子类变量 20 return this.age == p.age; 21 } 22 } 23 public class Demo1 { 24 public static void main(String[] args) { 25 Person p1 = new Person("王二",12); 26 Person p2 = new Person("王强",12); 27 System.out.println(p1);//toString 方法默认是返回包名.类名 @对象的哈希码 28 System.out.println(p1.equals(p2));//equals 默认的是放回俩个对象的地址是否相同 29 30 System.out.println(p1.hashCode()); 31 System.out.println(p2.hashCode()); 32 } 33 }