两个具有相同方法名称和签名的接口.但是由单个类实现,那么编译器将如何识别哪个接口的方法是什么?
例如:
interface A{
int f();
}
interface B{
int f();
}
class Test implements A, B{
public static void main(String... args) throws Exception{
}
@Override
public int f() { // from which interface A or B
return 0;
}
}
Run Code Online (Sandbox Code Playgroud) 我不这么认为,例如:
// A class implementing two interfaces Interface1 and Interface2.
// Interface1 has int x=10 and Interface2 has int x = 20
public class MultipleInterface implements Interface1, Interface2{
public void getX(){
System.out.println(x);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我们得到一个模棱两可的x.
虽然接口是解决方法模糊性的好方法,但我猜它们在变量的情况下会失败?
我对么?如果我遗失了什么,请赐教.