Với đoạn mã sau:Không thể sử dụng so sánh với cha-con trai-cháu trai thừa kế
public abstract class Participant {
private String fullName;
public Participant(String newFullName) {
this.fullName = new String(newFullName);
}
// some more code
}
public class Player extends Participant implements Comparable <Player> {
private int scoredGoals;
public Player(String newFullName, int scored) {
super(newFullName);
this.scoredGoals = scored;
}
public int compareTo (Player otherPlayer) {
Integer _scoredGoals = new Integer(this.scoredGoals);
return _scoredGoals.compareTo(otherPlayer.getPlayerGoals());
}
// more irrelevant code
}
public class Goalkeeper extends Player implements Comparable <Goalkeeper> {
private int missedGoals;
public Goalkeeper(String newFullName) {
super(newFullName,0);
missedGoals = 0;
}
public int compareTo (Goalkeeper otherGoalkeeper) {
Integer _missedGoals = new Integer(this.missedGoals);
return _missedGoals.compareTo(otherGoalkeeper.getMissedGoals());
}
// more code
}
Vấn đề là Goalkeeper
sẽ không complie.
Khi tôi cố gắng để biên dịch mã Eclipse ném:
The interface Comparable cannot be implemented more than once with
different arguments: Comparable<Player> and Comparable<Goalkeeper>
Tôi không cố gắng để so sánh với Player
, nhưng với Goalkeeper
, và chỉ với anh ta.
Tôi đang làm gì sai?
Xin vui lòng bạn có thể sửa chữa các vết lõm và khoảng trắng trong mã ví dụ của bạn. Rất khó đọc. –
Xong, xin lỗi! Tôi hy vọng nó không sao. – ron
Tại sao 'new String()' trong các hàm tạo? Chuỗi là bất biến! – fge