接口(interface) 1 具有方法 1、变量 x,接口(interface) 2 具有方法 1、变量 x。 为什么在第 1 行显示错误,而在第 2 行却没有显示错误?
interface interface1{
public int x =10;
public void method1();
}
interface interface2{
public int x =11;
public void method1();
}
public class Test implements interface1, interface2{
int y = x; // Line 1
@Override
public void method1() { //Line 2
}
}
最佳答案
“x”不明确,因为作用域中有两个,每个接口(interface)都有一个。相比之下,“method1()”则不然,因为根据 Java 规则,Test 中的定义满足提供两个接口(interface)中定义的实现的要求。
关于java - java中的多个接口(interface)具有相同的方法名称和变量名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14290902/