我在线程“main”java.lang.ClassCastException 中不断收到异常:studentscoresapp.Student cannot be cast to java.lang.Comparable 我无法弄清楚原因。也许有人可以看看并告诉我我在哪里搞砸了。我已经搞砸了几个小时,似乎我让程序变得更糟,而且我在网上找到的任何东西似乎都不起作用。谢谢
public class Student implements IComparable<Student>
{
private String lastName;
private String firstName;
private int score;
public Student(String lastName, String firstName, int score)
{
this.lastName = lastName;
this.firstName = firstName;
this.score = score;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the score
*/
public int getScore() {
return score;
}
/**
* @param score the score to set
*/
public void setScore(int score) {
this.score = score;
}
@Override
public int compareTo(Object obj)
{
Student i = (Student) obj;
if (i.lastName.equals(lastName)) {
return firstName.compareToIgnoreCase(i.firstName);
} else {
return lastName.compareToIgnoreCase(i.lastName);
}
}
}
我的 compareTo 界面覆盖
public interface IComparable<E> {
int compareTo(Object object);
}
和学生评分应用
public class StudentScoresApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Welcome message
System.out.println("Welcome to the Student Scores Application.");
System.out.println();
int count = ConsoleValidator.getInt("Enter number of students to enter: ", 0);
Student[] student = new Student[count];
for (int j = 0; count > j; j++)
{
student[j] = getItem();
System.out.println();
}
Arrays.sort(student);
for (Student i : student)
{
System.out.println(i.getLastName() + " " + i.getFirstName() + ": " + i.getScore() + "\n");
}
}
public static Student getItem()
{
String name = ConsoleValidator.getString("Student first name: ");
String last = ConsoleValidator.getString("Student last name: ");
int score = ConsoleValidator.getInt("Student score: ", 0);
Student j = new Student(name,last,score);
return j;
}
}