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 boolean equals(Object obj){
9 if(this==obj)//地址相等?
10 return true;
11 if(!(obj instanceof Person))//判断是否为Person的实例
12 return false;
13 Person per=(Person)obj;
14 if(per.name.equals(this.name)&&per.age==this.age){
15 return true;
16 }
17 else{
18 return false;
19 }
20 }
21 }
22 public class ObjectDemo {
23 public static void main(String[] args) {
24 Person per1=new Person("李新华 ",30);
25 Person per2=new Person("李新华 ",30);
26 System.out.println(per1.equals(per2)?"同":"不同");
27 System.out.println(per1.equals("hello")?"同":"不同");
28 }
29
30 }