スレッド「メイン」で例外が発生し続けます java.lang.ClassCastException: studentscoresapp.Student を 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;
}
}