简体   繁体   English

公共静态布尔等于方法

[英]public static boolean Equals Method

I have to objects. 我要反对。 Student and Course (both of which have this static equals method) I also have the driver which my professor wrote and I have been told not to touch. 学生和课程(均具有此静态equals方法)我也有我的教授编写的驱动程序,并且被告知不要触摸。 My goal is to make two arraylists of courses taken by the student, and current courses the student is taking. 我的目标是制作学生所修课程和学生正在修读的当前课程的两个列表。

My professor gave me all of the methods, I am supposed to fill in the bodies. 我的教授给了我所有的方法,我应该填写身体。

I double checked, and the equals method is copied exactly as he wants it to be. 我仔细检查了一下,然后完全按照他的意愿复制了equals方法。

This is a homework problem. 这是一个作业问题。

I need to fill in the body of this equals method. 我需要填写此equals方法的正文。 I have tried things like if(this.id == other.getID()) but I keep on receiving this error: error: non-static variable id cannot be referenced from a static context 我已经尝试过诸如if(this.id == other.getID())之类的事情,但是我一直收到此错误:错误:无法从静态上下文引用非静态变量id

I have tried using this.getID() instead but to no avail. 我尝试使用this.getID()代替,但无济于事。 I think this probably has something to do with this being static. 我认为这可能与静态有关。 (I am not allowed to change that.) (我不能更改它。)

What is the correct way to go about writing this? 编写此书的正确方法是什么? I am not going to include all of the getters and setters in the below code. 我不会在下面的代码中包含所有的getter和setter方法。

public class Student {

private String id;
private String firstName;
private String lastName;
private String major;
private String minor;
private ArrayList<Course> coursesTaken;
private ArrayList<Course> currentSemesterCourses;
private double gpa;

/**
    Course constructor
*/
public Student(String id, String firstName, String lastName, String major, String minor, ArrayList<Course> coursesTaken, ArrayList<Course> currentSemesterCourses) {
/*Your code goes here */
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.major = major;
    this.minor = minor;
    this.coursesTaken = coursesTaken;
    this.currentSemesterCourses = currentSemesterCourses;
}


public static boolean Equals(Object obj) {
//Your code goes here   
//base it on id firstName, lastName, major, minor and gpa
}

I think this probably has something to do with this being static. 我认为这可能与静态有关。 (I am not allowed to change that.) (我不能更改它。)

Then your assignment makes no sense whatsoever and there is no way to solve it. 这样,您的任务毫无意义,也无法解决。

If you are writing a static equals(Object) method, then you have no Student . 如果要编写静态equals(Object)方法,则没有Student There is only one object that got passed in, and it could be any type. 只有一个传入的对象,它可以是任何类型。 "Equals" is a question you ask about two things, and you don't even have two things to compare, you have only one thing that might not even be a Student . “等于”是您问两件事的一个问题甚至没有两件事可以比较,只有一件事甚至可能不是Student

The method you've been asked to write is like asking "Is this object equals?" 您被要求编写的方法就像问“此对象是否相等?”。 It makes exactly as little sense as that sentence. 它与那句话完全没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM