首页 > 解决方案 > IN主类不能调用子类

问题描述

import org.w3c.dom.ls.LSOutput;

public class Super {
    int x = 155;

    void display1() {
        System.out.println("values is " + x);
    }

    class Sub extends Super {
        int y = 587;

        void display2() {
            System.out.println("values was " + x);
            System.out.println("valus is. " + y);
        }

    }

}

***********主课**************

public class Main {
    public static void main(String[] args) {

        Super ss = new Super();
        ss.display1();


        //how to call sub class. it is giving error : super is not an enclosed class
        Super.Sub rr = new Super.Sub();
        rr.display2();

    }
}

标签: java

解决方案


代替

Super.Sub rr = new Super.Sub();

Super.Sub rr = new Super().new Sub();

或声明Substatic class.

检查以了解更多信息。


推荐阅读