equals方法重写遇到的问题

acm456 2009-01-22 12:23:31
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 TestEq {
public boolean equals(Object otherObject) {
if(this == otherObject) return true;
else {
if(otherObject == null) return false;
else {
if(this.getClass() != otherObject.getClass()) return false;
else {
if(otherObject instanceof Person) {
Person other = (Person)otherObject;
if(name == other.name && location == other.location) return true;
}
else {
if (otherObject instanceof Teacher) {
Teacher other = (Teacher)otherObject;
if(name == other.name && location == other.location && other.job == job)
return true;
}
}
}
}
return false;
}
}

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");
Teacher t3 = new Teacher("xiaogang","yunnan","yazi");
Teacher t4 = new Teacher("xiaogang","yunnan","yazi");
System.out.println(t3.equals(t4));
System.out.println(p1.Info());
System.out.println(p2.Info());
System.out.println(t1.Info());
System.out.println(t2.Info());
}
}
...全文
106 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzheng2001 2009-01-22
  • 打赏
  • 举报
回复
自己 debug 跟踪一下代码的运行就知道了.
mizukusa 2009-01-22
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20070123/16/9F8895F7-AA99-4422-81D5-64AC242052B7.html
bzwm 2009-01-22
  • 打赏
  • 举报
回复
怎么又是equals方法重写遇到的问题 ?

提问之前应该有相同问题已经被解决的吧。

不妨去看看啊,

而且代码MS都是一摸一样的。
acm456 2009-01-22
  • 打赏
  • 举报
回复
为什么结果一直是false呢
感觉应该是true啊,请大家帮忙找下原因,谢谢了
acm456 2009-01-22
  • 打赏
  • 举报
回复
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;
}

public boolean equals(Object otherObject) {
if(this == otherObject) return true;
if(otherObject == null) return false;
if(getClass() != otherObject.getClass())
return false;
else {
Person other = (Person)otherObject;
if(other.name == this.name && other.location == this.location) {
return true;
}
return false;
}
}

}

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 boolean equals(Object otherObject) {
if(!super.equals(otherObject)) return false;
else {
Teacher other = (Teacher)otherObject;
if(other.job == this.job) {
return true;
}
return false;
}
}

}

public class TestEq {
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");
Teacher p3 = new Teacher("xiaogang","yunnan","yazi");
Teacher p4 = new Teacher("xiaogang","yunnan","yazi");
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());
}
}

正确程序是这个,终于弄好了,怎么改结果都对了,谢谢大家
liang8305 2009-01-22
  • 打赏
  • 举报
回复
if(this.getClass() != otherObject.getClass()) return false;


这两个getClass出来的都是对象,你用==来比,自然就是false了

你完全可以去掉这个判断,用instendsof就够了
huanhuan_hyj 2009-01-22
  • 打赏
  • 举报
回复
TestEq类里面的name怎么来的
if(name == other.name && location == other.location && other.job == job)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧