equals方法的作用
用于对象之间的比较,返回true和false的结果
测试
//Students类
public class Student extends Object{
private String name;
private int chinese;
private int math;
@Override
public boolean equals(Object o) {
//如果a的地址等于c的地址则返回ture
if (this == o) return true;
//如果c传入是null或a的类名不等于c的类型则返回false
if (o == null || getClass() != o.getClass()) return false;
//向下转型
Student student = (Student) o;
//如果a的chinese不等于c的chines则返回false
if (chinese != student.chinese) return false;
//如果a的math不等于c的math则返回falsh
if (math != student.math) return false;
//如果a 的name不等于空则比较a的name和c的name,否则c的名字是空的
return name != null ? name.equals(student.name) : student.name == null;
}
}
//测试类
public class tyutry {
public static void main(String[] args) {
Student a=new Student("12",12,65);
Student c=new Student("12",12,65);
System.out.println(a.equals(c));
}
}