次のクラスのセットがあります。
public abstract class GSObject<T extends GSObject<T>> {
public abstract boolean matches(String toMatch);
//Other functions
public static <T extends GSObject<T>> T findMatch(List<T> objects, String toMatch){
//Code that iterates through the list, seeing if one matches;
}
}
public abstract class Phrase extends GSObject<Phrase> {
//More code
}
public class Request extends Phrase{
@Override
public boolean matches(String toMatch){
//Implementation of matches()
}
}
次のコマンドRequest.findMatch(allRequests,chat);
を実行すると、次のエラーが発生します。
Bound mismatch: The generic method findMatch(List<T>, String) of type GSObject<T> is not applicable for the arguments (List<Request>, String). The inferred type Request is not a valid substitute for the bounded parameter <T extends GSObject<T>>
私がそうPhrase.findMatch(allPhrases, chat);
しても、エラーはスローされません。つまり、これは二重継承に関係しています。GSObject を拡張するクラスを拡張するクラスで動作する別の静的関数を作成する必要がありますか?
GSObject をインターフェースにすることを検討しましたが、クラスで (抽象的ではなく) 定義したいクラスがいくつかあります。
(3 つのクラスのいずれかで) 不足しているものはありますか、または関数を定義するインターフェイスを作成する必要がありますかmatches()
(回避しようとしていること)?