0
私は制御できない外部ライブラリを使用しています。 それは、メソッドを実装するために私を必要とし が、私は必要とされるタイプ返す方法を知りません。私もこれを試しクラスを返すときの互換性のない型
public MyClass implements AnInterface<String> {
...
}
、 それ:
@Override
public Optional<Class<? extends AnInterface<?>>> getCustomClass() {
return Optional.of(MyClass.class);
// compiler is giving error at the above line:
// error: incompatible types: inference variable T has incompatible bounds
// equality constraints: Class<? extends AnInterface<?>>
// lower bounds: Class<MyClass>
// where T is a type-variable:
// T extends Object declared in method <T>of(T)
}
MyClassのを動作しません:
:@Override
public Optional<Class<? extends AnInterface<?>>> getCustomClass() {
Optional<Class<? extends AnInterface<?>> clazz = MyClass.class;
// this time the compiler gives error at the above line:
// error: incompatible types: Class<MyClass> cannot be converted to Class<? extends AnInterface<?>>
return clazz;
}
はまた、いずれかの動作しません別のものを試してみました210
@Override
public Optional<Class<? extends AnInterface<?>>> getCustomClass() {
Optional<Class<? extends AnInterface> clazz = MyClass.class;
return clazz;
// this time the compiler gives error at the above line:
// error: incompatible types: inference variable T has incompatible bounds
// equality constraints: Class<? extends PropertyEditor<?>>
// lower bounds: Class<CAP#1>
// where T is a type-variable:
// T extends Object declared in method <T>of(T)
// where CAP#1 is a fresh type-variable:
// CAP#1 extends PropertyEditor from capture of ? extends PropertyEditor
}
カスタムクラスを返す方法はありますか?
JDKのバージョンは:
EDIT 8u77:クラスは、次のように入れ子になっている:
のオーバーライドされたメソッドを実装するときに何らかの理由でコンパイラがワイルドカードで混乱しているpublic abstract class ParentClass<T> {
public abstract T method();
public class ImplementationClass implements ExternalLibraryInterface {
@Override
public Optional<Class<? extends AnotherExternalLibraryInterface<?>>> getCustomClass() {
return Optional.of(MyClass.class);
// compiler is giving error at the above line:
// error: incompatible types: inference variable T has incompatible bounds
// equality constraints: Class<? extends AnotherExternalLibraryInterface<?>>
// lower bounds: Class<MyClass>
// where T is a type-variable:
// T extends Object declared in method <T>of(T)
}
MyClass implements AnotherExternalLibraryInterface<String>{
...
}
}
}
外部インタフェースすべてのクラスを独自のファイルに移動した後、すべて正常に動作します。 Zefick初のソリューションは、実際に入れ子になったクラスの状況で動作します@解決として
しかし、私はそれをマークします:
return (Optional)(Optional.of(MyClass.class));
は1.8.0_74で再現することはできません。 'return Optional.of(MyClass.class);'は期待どおりに動作します。テスト: 'publicオプション >> getCustomClass(){ \t \t戻り値Optional.of(MyClass.class); \t} \tインタフェースAnInterface {} \t MyClassクラスはAnInterface {} ' –
Tunaki
@Tunakiを実現します。奇妙なことですが、質問のコードスニペット(簡略化されたバージョン)が実際に動作します。完全なバージョンのコードはコンパイルエラーを起こします。間違いなくコンパイルエラーの原因となっているリターンラインです。 Optional.empty()を返すときにビルドされます。 – zhywu