だから私はいくつかの多重継承Javaのことを深く掘り下げていたので、私の質問:
class Base {
static int x = 10;
}
interface Interface {
int x = 20;
}
class MultipleInheritance extends Base implements Interface {
int z = 2 * Interface.x; // here I have both variables from interface and base class
// I can use either "Interface" or "Base" to specify which 'x' variable I want to use
}
class TestClass {
void aMethod(MultipleInheritance arg) {
System.out.println("x = " + arg.x); // compiler error
// how to specify which 'x' variable I want to use here?
}
}
メソッド参照とは異なり、フィールド参照は多態的ではありません。これらは、コンパイル時に参照タイプによって解決されます。
オブジェクト参照を使用する必要がある場合は、参照タイプを自分でキャストして、どちらを使用するかを決定できます
System.out.println("x = " + ((Interface) arg).x);
System.out.println("x = " + ((Base) arg).x);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加