//答えは2つの理由がわかりません。メソッドがオーバーライドされている場合、オブジェクト/クラスインスタンス //メソッドは呼び出されません。基本的になぜSoccerPlayerの代わりにPersonクラスのcompareToを呼び出すのですか?なぜ答えは2ですか?事前に感謝compareToによって混乱しています
public class Person implements Comparable {
private String name;
public Person(String name) { this.name = name; }
public String getName() { return name; }
public boolean equals(Object other) {
return other != null && name.equals(((Person) other).name);
}
public int compareTo(Object other) {
return name.compareTo((Person other).name);
}
public int hashCode() { return name.hashCode(); }
}
public class SoccerPlayer extends Person {
private int numGoals;
public SoccerPlayer(String name, int n) {
super(name);
numGoals = n;
}
public int getNumGoals() { return numGoals; }
public void score() [ numGoals++; }
public int compareTo(SoccerPlayer other) {
return getNumGoals() - other.getNumGoals();
}
public String toString() {
return getName() + "/" + getNumGoals();
}
}
次のコードの結果はどうなりますか? //なぜ2?
Person players[] = { new SoccerPlayer("Mia Hamm", 7), new SoccerPlayer("Kristine Lilly", 6) };
System.out.println(players[0].compareTo((SoccerPlayer) players[1])); // Line ***
a。 Person:other.nameにアクセスできないクラスの構文エラー
b。 SoccerPlayerクラスの構文エラー:compareToが再定義されました
c。 ClassCaseException on Line ***
d。エラーなしでコンパイル、表示1
e。エラーなしでコンパイルして表示する2
これらの回答はどちらも生の種類を使用するため、間違っています。 –