考虑我有两个接口具有相同的变量名称,并且此变量名称与两个接口名称中的一个相同。
例如,Java接口变量中的歧义
interface Parent1{
public static final String Parent1= "VALUE1"; // Variable Parent1 is same as interface name
}
interface Parent2{
public static final String Parent1= "VALUE2";
}
说如果我有一个实现上述两个接口的类,如果我需要访问的变量Parent1接口Parent1,我怎么能访问。
例如,
class Child implements Parent1, Parent2{
void myCode(){
System.out.println(Parent1.Parent1); // Does not compile. Because Parent1(before dot) is considered as variable
System.out.println(Parent2.Parent1); // Does compile
}
}
我知道变量名称不遵循标准。但是想要了解java如何克服这种模糊性。
编辑:
人们说它正在工作(在评论中)。但是当我执行它时说
/Child.java:9: error: reference to Parent1 is ambiguous
System.out.println(Parent1.Parent1); // Does not compile. Because Parent1(before dot) is considered as variable
^
both variable Parent1 in Parent1 and variable Parent1 in Parent2 match
/Child.java:9: error: cannot find symbol
System.out.println(Parent1.Parent1); // Does not compile. Because Parent1(before dot) is considered as variable
^
symbol: variable Parent1
location: variable Parent1 of type String
2 errors
为我编译好。 –
它正在工作,你面临问题。它正在工作 – iamsankalp89
您正在使用哪个版本的Java以及哪种编译器?另外...哪一个是>>你使用@JoeC? –