class Person {
private String name;
private String location;
Person(String name) {
this.name = name;
location = "haerbin";
}
Person(String name,String location) {
this.name = name;
this.location = location;
}
public String Info() {
return "name:"+name+","+"location:"+location;
}
}class Teacher extends Person {
private String job;
Teacher(String n,String j) {
this(n,"beijing",j);
}
Teacher(String name,String location,String job) {
super(name,location);
this.job = job;
}
public String Info() {
return super.Info()+","+"job:"+job;
}
}public class TestE {
public static void main(String[] args) {
Person p1 = new Person("leisao");
Person p2 = new Person("dachao","jilin");
Teacher t1 = new Teacher("jinfeng","gongwuyuan");
Teacher t2 = new Teacher("xiaogang","yunnan","yazi");
Person p3 = new Person("leisao");
Person p4 = new Person("leisao");
System.out.println(p3.equals(p4));
System.out.println(p1.Info());
System.out.println(p2.Info());
System.out.println(t1.Info());
System.out.println(t2.Info());
}
}为什么p3和p4比较打印结果会false呢,不是说字符串比较只要排序相同就可以嘛
想了好久,没想出来,试验了好几种equals方法的重写,也写不好
求高人能帮忙给写个equals方法,能让p3和p4比较打印出true
private String name;
private String location;
Person(String name) {
this.name = name;
location = "haerbin";
}
Person(String name,String location) {
this.name = name;
this.location = location;
}
public String Info() {
return "name:"+name+","+"location:"+location;
}
}class Teacher extends Person {
private String job;
Teacher(String n,String j) {
this(n,"beijing",j);
}
Teacher(String name,String location,String job) {
super(name,location);
this.job = job;
}
public String Info() {
return super.Info()+","+"job:"+job;
}
}public class TestE {
public static void main(String[] args) {
Person p1 = new Person("leisao");
Person p2 = new Person("dachao","jilin");
Teacher t1 = new Teacher("jinfeng","gongwuyuan");
Teacher t2 = new Teacher("xiaogang","yunnan","yazi");
Person p3 = new Person("leisao");
Person p4 = new Person("leisao");
System.out.println(p3.equals(p4));
System.out.println(p1.Info());
System.out.println(p2.Info());
System.out.println(t1.Info());
System.out.println(t2.Info());
}
}为什么p3和p4比较打印结果会false呢,不是说字符串比较只要排序相同就可以嘛
想了好久,没想出来,试验了好几种equals方法的重写,也写不好
求高人能帮忙给写个equals方法,能让p3和p4比较打印出true
重写equals就是比较Person对象的所有域是否都相同网上找到的资料,希望对你有帮助
重写equals()方法
一、规范
Java语言规范要求equals方法具有下面的特性:
1) 自反性:对于任何非空引用x, x.equals(x) 应该返回true。2) 对称性:对于任何引用x和y,如果x.equals(y)返回true,那么y.equals(x)也应该返回true。3) 传递性:对于任何引用x、y和z,如果x.equals(y)返回true,y.equals(z)返回true,那么x.equals(z)也应该返回true。4) 一致性:如果x和y引用的对象没有发生变化,那么反复调用x.equals(y)应该返回同样的结果。5) 对于任意非空引用x,x.equals(null)应该返回false。二、建议
下面给出编写一个完美的equals方法的建议:1) 显式参数命名为otherObject,稍后需要将它转换成另一个叫做 other的变量。2) 检测this与otherObject是否引用同一个对象: if (this == otherObject) return true; 这条语句只是一个优化。实际上,这是一种经常采用的形 式。因为计算这个等式要比一个一个地比较类中的域所付 出的代价小得多。3) 检测otherObject是否为null,如果为null,返回false。这项 检测是很必要的。 if (otherObject == null) return false; 比较this与otherObject是否属于同一个类。如果equals的语 义在每个子类中有所改变,就使用getClass检测: if (getClass() != otherObject.getClass()) return false; 如果所有的子类都拥有统一的语义,就使用instanceof检测: if (! (otherObject instanceof ClassName)) retrun false;4)将otherObject转换为相应的类类型变量: ClassName other = (ClassName)otherObject;5) 现在开始对所有需要比较的域进行比较了。使用 == 比较 基本类型域,使用equals比较对象域。如果所有的域都匹 配,就返回ture;否则返回false。 return field1 == other.field1 && field2.equals(other.field2) && ……;
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
一并提供hashcode()
* @author bzwm
*
*/
class Person {
private String name; private String location; Person(String name) {
this.name = name;
location = "haerbin";
}
/**
* @return the name
*/
public String getName() { //追加
return name;
} /**
* @param name the name to set
*/
public void setName(String name) {//追加 this.name = name;
} Person(String name, String location) {
this.name = name;
this.location = location;
} public String Info() {
return "name:" + name + "," + "location:" + location;
} public boolean equals(Object o){//追加
if(!(o instanceof Person)){
return false;
}else{
return name.equals(((Person)o).getName());
}
}
}class Teacher extends Person {
private String job; Teacher(String n, String j) {
this(n, "beijing", j);
} Teacher(String name, String location, String job) {
super(name, location);
this.job = job;
} public String Info() {
return super.Info() + "," + "job:" + job;
}
}public class TestE {
public static void main(String[] args) {
Person p1 = new Person("leisao");
Person p2 = new Person("dachao", "jilin");
Teacher t1 = new Teacher("jinfeng", "gongwuyuan");
Teacher t2 = new Teacher("xiaogang", "yunnan", "yazi");
Person p3 = new Person("leisao");
Person p4 = new Person("leisao");
System.out.println(p3.equals(p4));
System.out.println(p1.Info());
System.out.println(p2.Info());
System.out.println(t1.Info());
System.out.println(t2.Info());
}
}