定义Teacher类,属性:String name,int age,char gender,String nu ,在这个类中重写equals方法

在这里插入图片描述
代码:
package cn.tedu.object;
public class Teacher {
String name;
int age;
char gender;
String nu;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Teacher teacher = (Teacher) o;
if (age != teacher.age) return false;
if (gender != teacher.gender) return false;
if (name != null ? !name.equals(teacher.name) : teacher.name != null) return false;
return nu != null ? nu.equals(teacher.nu) : teacher.nu == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
result = 31 * result + (int) gender;
result = 31 * result + (nu != null ? nu.hashCode() : 0);
return result;
}
/* @Override
public boolean equals(Object obj) {
if(thisobj){
return true;
}
if(obj
null){
return false;
}
if(this.getClass()!=obj.getClass()){
return false;
}
Teacher t=(Teacher) obj;
if(this.age!=t.age){
return false;
}
if(this.gender!=t.gender){
return false;
}
if(!(this.namet.name||this.name!=null&&this.name.equals(t.name))){
return false;
}
if(this.nu
t.nu||this.nu!=null&&this.nu.equals(t.nu)){
return true;
}
return false;
}*/
}

代码图:
定义Teacher类,属性:String name,int age,char gender,String nu ,在这个类中重写equals方法_第1张图片定义Teacher类,属性:String name,int age,char gender,String nu ,在这个类中重写equals方法_第2张图片

你可能感兴趣的:(Object,object,java)