//测试类 |
public class StudentDemo { |
public static void main(String[] args) { |
Student s1 = new Student( "000" , "张三" , 18 ); |
Student s2 = new Student( "000" , "张三" , 18 ); //随便改 |
boolean flag = s1.equals(s2); |
System.out.println(flag); |
} |
} |
//实现类Student |
public class Student { |
//私有成员 |
private String id; |
private String name; |
private int age; |
//带参构造函数 |
public Student(String id, String name, int age) { |
super (); |
this .id = id; |
this .name = name; |
this .age = age; |
} |
//无参构造函数 |
public Student() { |
super (); |
// TODO Auto-generated constructor stub |
} |
public String getId() { |
return id; |
} |
public void setId(String id) { |
this .id = id; |
} |
public String getName() { |
return name; |
} |
public void setName(String name) { |
this .name = name; |
} |
public int getAge() { |
return age; |
} |
public void setAge( int age) { |
this .age = age; |
} |
@Override |
public String toString() { |
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]" ; |
} |
public boolean equals(Object obj) { |
if ( this == obj) |
return true ; |
if (obj == null ) |
return false ; |
if (getClass() != obj.getClass()) |
return false ; |
Student s = (Student) obj; |
if (id != s.id) |
return false ; |
if (age != s.age) |
return false ; |
if (name == null ) { |
if (s.name != null ) |
return false ; |
} else if (!name.equals(s.name)) |
return false ; |
return true ; |
} |
} |