public class Testified {
public static void main(String[] args) {
Student s2=new Student(1,"赵四",23);
Student s3=new Student(1,"赵四",23);
System.out.println(s2.equals(s3));
System.out.println(s2);
}
}
public class Student {
private int id;
private String name;
private int age;
@Override
public boolean equals(Object obj){
//如果当前对象 要对比的对象是一个对象,则返回值为true
if (this==obj){
return true;
}
if (obj instanceof Student){//如果要对比的对象是学生类型,在对比id和名字
Student stu =(Student)obj;//向下转型
//当前对象中的id和name yu 要对比的对象中id和name是否相同
if (this.id==stu.getId()&&this.name==stu.getName()){
return true;
}
return false;
}
return false;
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public Student() {
}
public int getId() {
return id;
}
public void setId(int 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 +
'}';
}
}