public class Person{
public static void main(String[] args) {
Person p1 = new Person("p1","femail",15) ;
Person p2 = new Person("p","mail", 20) ;
Person p3 = new Person("p","mail", 20) ;
System.out.println(p1.equals(p2)) ;
System.out.println(p3.equals(p2)) ;
}
public Person() {} ;
public Person(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public boolean equals(Object otherObject) {
//检测两个引用是否指向同一对象
if( this == otherObject ) return true ;
//检测otherObject是否为空
if( null == otherObject) return false ;
//检测是否为同一个类
if( getClass() != otherObject.getClass() ) return false ;
//将otherObject转成Person类
Person person = (Person)otherObject ;
return this.getName().equals(person.getName())
&& this.getSex().equals(person.getSex())
&& this.getAge() == person.getAge() ;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String name = null;
public String sex = null ;
public int age = 0 ;
}