java - 访问嵌套接口(interface)数据变量

标签 java interface nested

我无法访问此代码的变量 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/

相关文章:

java - Sonarqube 6.7x 的安全插件

java - 如何在 Java 中正确嵌套多个 ArrayLists/Maps?

java - 使用 printf 打印列

java - 我正在尝试弄清楚如何创建一个菜单,该菜单允许我在选择该选项后重新启动程序

c# - 如何在 C# 中编写实现给定接口(interface)的通用容器类?

java - 用作接口(interface)替代的内部类示例

PHP嵌套菜单安全问题

c# - 类嵌套和访问修饰符

java.lang.NoClassDefFoundError : org/apache/kafka/clients/producer/Producer

c++ - 在 C++ 中通过接口(interface)的最佳方式?