【问题标题】:I am new to programming and I can't tell if this error is occurring because of my syntax我是编程新手,我不知道是否由于我的语法而发生此错误
【发布时间】:2018-01-14 17:13:38
【问题描述】:
package studentclient;

import java.util.Scanner;
public class StudentClient {

public static void main(String[] args) {

    /* Declare two object references of type Student s1 and s2 and instantiate two Student objects passing three arguments to the constructor for the class. Use different values for each class object */

    Student s1 = new Student("Dick Grayson", "666-66-6666", 3.80 );
    Student s2 = new Student("Jason Todd", "666-77-7777", 3.50 );

    Scanner scan = new Scanner(System.in);

    /* Output the name, social security number and GPA of the student from object reference s1 using the appropriate accessor methods(Get methods) to obtain the data */
    System.out.println("Student Information");

    System.out.println("Student Name: " + s1.getName());

    System.out.println("Student Social Security Number: " + s1.getSsn());

    System.out.println("Student GPA: " + s1.getGpa());

    /* Output the name, social security number and GPA of the student from object reference s2 using the toString method to return the data */
    System.out.println("Student Information");

    System.out.print(s2);

    /* Using the equals method and a selection control structure (if statement), compare objects s1 and s2 and output an appropriate message indicating if the objects are equal */

    if(s1.equals(s2)){

    System.out.println("Student's identities are equal!");

    }else {

    System.out.println("Student's identities are not equal!");

    }
    /* Using the appropriate mutator methods(Set Methods) on student object s2, change the name, social security number and GPA to the same values as in object s1. Use the set methods. */
       System.out.println("Enter new Student Information for Student 2: ");

       s2.setName("Dick Grayson");
       System.out.println("Student Name: " + s2.getName());

       s2.setSsn("666-66-6666");
       System.out.println("Student Social Security Number: " + s2.getSsn());

       s2.setGpa( 3.80 );
       System.out.println("Student GPA: " + s2.getGpa());

       /* Again, using the equals method and a selection control structure (if statement), compare objects s1 and s2 and output an appropriate message indicating if the objects are equal */
       if(s1.equals(s2)){
           System.out.println("Student's identities are equal!");
       }else{
           System.out.println("Student's identities are not equal!");
       }
    }
}

问题在于,对于第二组信息,s2 显然等于 s1,但我的结果一直说它们不相等。我该怎么办? 当我运行它时,我会得到这些结果

学生信息 学生姓名:迪克·格雷森 学生社会安全号码:666-66-6666 学生平均绩点:3.8 学生信息 姓名:杰森·托德 社会安全号码:666-77-7777 平均绩点:3.5 学生身份不平等! 输入学生 2 的新学生信息: 学生姓名:迪克·格雷森 学生社会安全号码:666-66-6666 学生平均绩点:3.8 学生身份不平等! 构建成功(总时间:0 秒)

【问题讨论】:

标签: java syntax


【解决方案1】:

您正在将equals 方法与您的自定义对象Student 一起使用。

您需要在 Student 类中覆盖它。在您的情况下,如果您想将s1 的所有字段与s2 匹配,您的实现应该是这样的:

//Overriding equals
 public boolean equals(Object obj) 
  {
    if (this == obj) return true;
    if (obj == null) return false;
    if (this.getClass() != obj.getClass()) return false;
    Student that = (Student) obj;
    if (!this.name.equals(that.name)) return false;
    if (!this.ssn.equals(that.ssn)) return false;
    if (!this.gpa.equals(that.gpa)) return false;
    return true;
  }
}

执行s1.equals(s2) 时,这样做会给您预期的结果。

但是,例如,如果您尝试将这些相同的对象 s1s2 添加到 Set,它将认为它们是不同的。原因是,两者的哈希码会不同。

因此,当您覆盖equals 时,您还应该覆盖hashcode 方法。

它的示例实现,考虑到您的 Id 类中有一些 Id 字段是:

@Override
public int hashCode()
{
    final int PRIME = 31;
    int result = 1;
    result = PRIME * result + getId();
    return result;
}

您应该使用相同的字段来实现equalshashcode 方法。

完成此操作后,您的代码应该会给您预期的结果。

希望有帮助!

【讨论】:

  • equals 方法中小心使用getClass。如果您有一些使用相同比较方法的子类(因此不要重新定义该方法),这将失败,因为您可以将Student 与他的子类之一进行比较,即使它们应该是等效的,这也不起作用。 (如果你真的想使用instanceof
猜你喜欢
  • 2018-05-31
  • 2022-01-20
  • 2018-08-09
  • 2021-02-08
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
  • 2014-07-11
  • 2016-02-27
相关资源
最近更新 更多