考慮我有兩個接口具有相同的變量名稱,並且此變量名稱與兩個接口名稱中的一個相同。
例如,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? –