您的位置:首页 > 编程语言 > Java开发

Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

2016-07-11 16:19 806 查看
问题:No enclosing instance of type Three is accessible. Must qualify the allocation with an enclosing instance of type Three (e.g. x.new A() where x is an instance of Three).

代码:public class Three {

public static void main(String[] args) {
Person person=new Person();
System.out.println(person.name);
}

public class Person{
public  String name="Person";
int age=0;
}

public class Child extends Person{
public String grade;
}

}

原因:内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。所以在不做其他变动的情况下,最简单的解决办法是将public
class改为public static class.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: