我无法访问此代码的变量 i
:
interface three{
void how();
interface two{
int i=2;
void what();
}
}
class one implements three,two{
public void how(){
System.out.println("\nHow! i = " + i);
what();
}
public void what(){
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]){
one a = new one();
a.how();
a.what();
}
}
生成的错误是:
one.java:17: error: cannot find symbol
System.out.println("\nWhat! i = " + i);
symbol: variable i
location: class one
最佳答案
您应该在外部创建接口(interface),以便其他类可以访问它。
interface three {
void how();
}
interface two {
int i = 2;
void what();
}
public class one implements three, two {
public void how() {
System.out.println("\nHow! i = " + i);
what();
}
public void what() {
System.out.println("\nWhat! i = " + i);
}
public static void main(String args[]) {
one a = new one();
a.how();
a.what();
}
}
关于java - 访问嵌套接口(interface)数据变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44326137/