2016-03-10 25 views
0

我必须对象。 学生和课程(两者都有这个静态等于方法) 我也有我的教授写的司机,我被告知不要碰。 我的目标是制作两个由学生组成的课程列表,以及学生正在学习的当前课程。公共静态布尔等于方法

我的教授给了我所有的方法,我应该填写尸体。

我重复检查,等于方法复制完全按照他希望的那样。

这是一个家庭作业问题。

我需要填写这个等于方法的正文。 我已经试过的东西一样,如果(this.id == other.getID()),但我不断收到此错误: 错误:非静态变量ID不能从静态上下文中引用

我已经尝试使用this.getID()代替,但无济于事。我认为这可能与这是静态的有关。 (我不允许改变这种情况。)

写这篇文章的正确方法是什么?我不打算在下面的代码中包含所有的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 
} 
+0

编写外部类并提供比较机制。 – mlewandowski

+0

有没有机会让这种方法失效?这不在我的指导之中,但我依稀记得我们在讲课中排除一些方法。 – Josh

+5

你需要和你的助教交谈。 equals方法不是静态的,它们也不是“Equals”。如果有人想让你做一个静态的比较方法,那就需要像'public static boolean Equals(Student student2,Student student1)' – bmargulies

回答

5

I think this probably has something to do with this being static. (I am not allowed to change that.)

然后你的任务是没有任何意义,也没有办法解决它。

如果你正在编写一个静态的equals(Object)方法,那么你的没有Student只有一个传入的对象,它可以是任何类型。 “Equals”是一个问题,你询问两件事情,,你甚至没有两件事要比较,你只有一件事可能不是Student

你被要求写的方法就像问“这个对象是否等于?”它和这句话一样毫无意义。

+0

你是否认为他可能想要一种不同于传统的平等的方法? – Josh

+1

@Josh如果他想要一个静态方法,它应该有两个参数,而不是一个。但任何类型的单参数静态等值方法都是完全没有意义的。 –