如何实例化非静态内部类?

我有一个内部类,我想从父类的静态方法中实例化如下:

public class MyClass {
    public class B {
        private int v;
        B(int x) {
            v = x;
        }

        public void pr() {
            System.out.println(this.v);
        }
    }

    public static void main(String args[]) {
      int x=10;
      int y=25;
      int z=x+y;
    B bb = new B(3);
      System.out.println("Sum of x+y = " + z);
    }
}

它抛出如下错误:

error: non-static variable this cannot be referenced from a static context
    B bb = new B(3);
           ^
1 error
alwvp 回答:如何实例化非静态内部类?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2925230.html

大家都在问