手记

JAVA在编写内部类时No enclosing instance of type intel is accessible.

public class intel {

    public static void main(String[] args) {
        PlaceName p1 = new PlaceName(1,"陕西",0);
        Integer s = new Integer(4);
        System.out.println(s);
    }
    public class PlaceName{
        public int pId;
        public String pName;
        public int partId;
        public PlaceName(int pId, String pName, int partId) {
//          super();
            this.pId = pId;
            this.pName = pName;
            this.partId = partId;
        }

    }
}

刚写好一个内部类,在main方法中实例PlaceName的时候出现错误,网上查阅了资料之后发现是main方法之前是有一个static修饰的,需要在调用之前实例化intel类,
PlaceName p1 = new intel().new PlaceName(1,"陕西",0);
这样改过之后就不会有错了,
还有一种方法是给PlaceName内部类加上static修饰

0人推荐
随时随地看视频
慕课网APP