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());
}
}

解决方案 »

  1.   

    为什么结果一直是false呢
    感觉应该是true啊,请大家帮忙找下原因,谢谢了
      

  2.   

    怎么又是equals方法重写遇到的问题  ?提问之前应该有相同问题已经被解决的吧。不妨去看看啊,而且代码MS都是一摸一样的。
      

  3.   

    http://topic.csdn.net/u/20070123/16/9F8895F7-AA99-4422-81D5-64AC242052B7.html
      

  4.   

    自己 debug 跟踪一下代码的运行就知道了. 
      

  5.   

    TestEq类里面的name怎么来的
    if(name == other.name && location == other.location && other.job == job) 
      

  6.   

    if(this.getClass() != otherObject.getClass()) return false; 
    这两个getClass出来的都是对象,你用==来比,自然就是false了你完全可以去掉这个判断,用instendsof就够了
      

  7.   

    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());
    }
    }正确程序是这个,终于弄好了,怎么改结果都对了,谢谢大家