jdk8
に次のディスカッションがあります。
class D extends C<Class<String>> implements I {
}
は次のようにインターフェイスI
を実装する必要があります:
class D extends C<Class<String>> implements I {
@Override
public void m(Class arg) {
}
}
は.AccordingインタフェースI
でメソッドを実装する必要がある理由を見てみましょうあなたのコードで
JLS8.4.8.1
:
An instance method mC declared in or inherited by class C, overrides from C another
method mI declared in an interface I, iff all of the following are true:
• I is a superinterface of C.
• mI is an abstract or default method.
• The signature of mC is a subsignature (§8.4.2) of the signature of mI.
上記のコードで
、mC
は、クラスD
でvoid m(Class<String> arg)
あるmI
がvoid m(Class arg)
あり、subsignature
の仕様を参照:
And the `JLS8.4.2` explain the subsignature between two method:
The signature of a method m1 is a subsignature of the signature of a method m2 if
either:
• m2 has the same signature as m1, or
• the signature of m1 is the same as the erasure (§4.6) of the signature of m2.
上記の明細書によれば、void m(Class<String> arg
はのサブシグニチャはありませんvoid m(Class arg)
ですが、void m(Class arg)
はvoid m(Class<String> arg
のサブシグニチャです。したがって、クラスD
はインターフェイスを実装する必要があります3210を明示的に示します。
はそれを説明するには、次のコードを参照してください。
class C {
public void m(Class arg) {
}
}
interface I<T> {
void m(T arg);
}
class D extends C implements I<Class<String>> {
}
それはOKコンパイルします。ためJLS8.4.8
の
秒、競合する方法が存在しない理由は、クラスの継承を説明:
A class C inherits from its direct superclass all concrete methods m (both static and instance) of the superclass for which all of the following are true:
• m is a member of the direct superclass of C.
• m is public, protected, or declared with package access in the same package as C.
• No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of m.
は、上記の仕様によれば、T
のClass<String>
パラメータを持つ方法void m(T arg)
がvoid m(Class<String> arg)
に変換すること。変換されたメソッドはvoid m(Class arg)
に消去されますが、このメソッドはクラスD
に存在するため、クラスD
はメソッドvoid m(T arg)
をクラスC
に継承しません。
まあクラスソリューションはI 'I'にタイプを追加し、私に(クラスを ''持つMにおそらくある –
クラスと同じ型ではありません。この場合は
–