为什么即使输出相同,s1也不等于s3?

时间:2015-10-21 13:02:25

标签: java

/* Student class with a constructor that initialises the name, gender, and degreeProgramme. */

public class Student {

    public static void main(String[] args){
        Student s1 = new Student("a", "b", "c", "d");
        System.out.println(s1.toString());

        s1.setName("Mary Jones");
        s1.setGender("female");
        s1.setStudentID("0564");;
        s1.setDegreeProgramme("History");


        Student s2 = new Student("Mary jones", "female", "0564", "History");
        Student s3 = new Student("Mary Jones", "female", "0564", "History");
        System.out.println(s1);
        System.out.println(s3);
        System.out.println(s2);
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
    }

    private String name; /* instance variable */
    private String gender; /* instance variable */
    private String studentID; /* instance variable */
    private String degreeProgramme; /* instance variable */

    /* Student constructor that receives 4 parameters */
    public Student(String name, String gender, String studentID, String degreeProgramme){

        this.name = name; /* assigns name to instance variable name */
        this.gender = gender; /* assigns gender to instance variable gender */
        this.studentID = studentID; /* assigns studentID to instance variable studentID */
        this.degreeProgramme = degreeProgramme; /* assigns degreeProgramme to instance variable degreeProgramme */


    }
    /* method that returns the name of the student */
    public String getName(){
        return name;
    }

    /* method that returns the gender of the student */
    public String getGender(){
        return gender;
    }
    /* method that returns the degree programme of the student */
    public String getDegreeProgramme(){
        return degreeProgramme;
    }

    /* method that returns the student ID */
    public String getStudentID(){
        return studentID;
    }

    /* method that sets the name of the student */
    public void setName(String name){
        this.name = name;
    }
    /* method that sets the student ID */
    public void setStudentID(String studentID){
        this.studentID = studentID;
    }

    /* method that sets the gender of the student */
    public void setGender(String Gender){
        this.gender = Gender;
    }

    /* method that sets the degree programme of the student */
    public void setDegreeProgramme(String degreeProgramme){
        this.degreeProgramme = degreeProgramme;
    }

    /* method that returns the name, gender, and degree programme of the student */
    public String toString(){
        String studentInfo = "["+name+ ", " +gender+ ", ID: " +studentID+ ", " +degreeProgramme+"]" ;
        return studentInfo;
    }


} /* end class Student */

任何人都可以告诉我为什么行" System.out.println(s1.equals(s3));"返回输出" false"即使输出相同?我一直试图弄清楚它10个小时,似乎无法找出原因。

3 个答案:

答案 0 :(得分:3)

你需要@override并编写自己的equals方法。目前正在检查两个对象是否相同。您需要重写它以检查两个对象是否具有相同的输出。

答案 1 :(得分:1)

目前,您正在使用从java.lang.Object类继承的equals方法,该方法实现如下:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Student)) return false;

    Student student = (Student) o;

    if (name != null ? !name.equals(student.name) : student.name != null) return false;
    if (gender != null ? !gender.equals(student.gender) : student.gender != null) return false;
    if (studentID != null ? !studentID.equals(student.studentID) : student.studentID != null) return false;
    return !(degreeProgramme != null ? !degreeProgramme.equals(student.degreeProgramme) : student.degreeProgramme != null);
}

它检查this和obj是否引用同一个实例。在你的情况下,s1和s3是Student的不同实例,因此s1.equals(s3)返回false。

您可以实现自己的equals方法来实现您的目的,例如:

{{1}}

答案 2 :(得分:0)

因为等于行为不同。

如果您尝试这样做,那将是真的:

String s1String = s1.toString();
String s3String = s3.toString();
s1String(equals(s3String));
//or just simply
s1.toString().equals(s3.toString());

正如已经指出的那样,您可以更改equals方法(如果您不完全确定自己在做什么以及可能产生什么后果,则可能存在潜在危险)或者您可以编写自己的方法来检查是否存在属性是一样的,我建议。

在您的学生班中,您可以添加此

public boolean hasSameProperties(Student student){
  return student.toString().equals(this.toString());
}

然后在你的main方法中,你可以写

的System.out.println(s1.hasSameProperties(S2));