zoukankan      html  css  js  c++  java
  • Java编译时出现No enclosing instance of type XXX is accessible.

    今天在编译Java程序的时候出现以下错误:

    No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main).

    我原来编写的源代码是这样的:

    public class Main 
    {
    class Dog //定义一个“狗类”
    {
    private String name;
    private int weight;
    public Dog(String name, int weight) 
    {
    this.setName(name);
    this.weight = weight;
    }
    public int getWeight() 
    {
    return weight;
    }
    public void setWeight(int weight) 
    {this.weight = weight;}
    public void setName(String name)
    {this.name = name;}
    public String getName() 
    {return name;}
    }
    public static void main(String[] args)
    {
    Dog d1 = new Dog("dog1",1);

    }
    }

    出现这个错误的时候,我一直不太理解。

    在借鉴别人的解释之后才恍然大悟。

    在代码中,我的Dog类是定义在Main中的内部类。Dog内部类是动态的内部类,而我的main方法是static静态的。

    就好比静态的方法不能调用动态的方法一样。

    有两种解决办法:

    第一种:

    将内部类Dog定义成静态static的类。

    第二种:

    将内部类Dog在Main类外边定义。

    修改后的代码:

    第一种:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public class Main
    {
        public static class Dog
        {
            private String name;
            private int weight;
            public Dog(String name, int weight)
            {
                this.setName(name);
                this.weight = weight;
            }
            public int getWeight()
            {
                return weight;
            }
            public void setWeight(int weight)
            {this.weight = weight;}
            public void setName(String name)
            {this.name = name;}
            public String getName()
            {return name;}
        }
        public static void main(String[] args)
        {
            Dog d1 = new Dog("dog1",1);
        }
    }

    第二种:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    public class Main
    {
        public static void main(String[] args)
        {
            Dog d1 = new Dog("dog1",1);
        }
    }
     
    class Dog
    {
            private String name;
            private int weight;
            public Dog(String name, int weight)
            {
                this.setName(name);
                this.weight = weight;
            }
            public int getWeight()
            {
                return weight;
            }
            public void setWeight(int weight)
            {this.weight = weight;}
            public void setName(String name)
            {this.name = name;}
            public String getName()
            {return name;}
    }

     

  • 相关阅读:
    【deep learning精华部分】稀疏自编码提取高阶特征、多层微调完全解释及代码逐行详解
    【machine learning通俗讲解code逐行注释】之线性回归实现
    softmax实现(程序逐句讲解)
    softmax回归(理论部分解释)
    AtomicInteger小小的理解
    jdk8新特性之lambda expressions
    i++ 与 ++i 的从字节码层面看二者的区别
    jdk8永久代从方法区移除的验证
    复杂事件处理引擎—Esper 处理模型
    复杂事件处理引擎—Esper参考(事件部分)
  • 原文地址:https://www.cnblogs.com/lucky-star-star/p/4418118.html
Copyright © 2011-2022 走看看