昨日この投稿を見ました: How I instantiate? コードを含める
ユーザーは、ジェネリック クラスのコンストラクター型 X を、コンストラクター IA に渡されるオブジェクトの型と一致させることができませんでしたが、<X extends IA>
.
X
M コンストラクターの型を からに変更する必要がある場合、ジェネリックの全体のポイントが役に立たなくなるため、提供された唯一の答えが本当に好きではありませんでしたIA<X>
。M
確かにこれがジェネリック型が<X extends IA>
??である理由です。
この基本的な例で (抑制された警告なしで) ジェネリックを使用する方法は本当にありませんか?
public interface IA<X extends IA<X>>{}
public class A<X extends IA<X>> implements IA<X>{}
public class W<X extends IA<X>>{}
public class M<X extends IA<X>> extends W<X>{
X anx;
public M(X x){} //Type X here is not compatibile with IA in the test code
}
//Testing code in a different class
public <X extends IA<X>> void check() {
IA<X> a = new A<X>();
W<X> s = new M<X>(a); //Doesn't compile because IA<X> is not the same as 'X', even though <X extends IA>
W<X> s = new M(a); //Compiles, but with suppressed warnings
X a = new A<X>(); //Doesnt compiler (ignoring dupicate 'a' variable)
W<X> s = new M<X>(a); compiles
}
「拡張」を含むあらゆる場所にIAを含めるように編集