Java 没有可访问的封闭实例。必须使用类型的封闭实例限定分配(egxnew A(),其中 x 是 的实例)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47541459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:38:51  来源:igfitidea点击:

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

javaclass

提问by Nick Ninov

I'm new to programming and I'll be studying it next year in uni. In my public static void main ... I can't create a new SimpleCircle. This error occurs only on my circle. Thank you very much for the help! :)

我是编程新手,明年我将在大学学习。在我的 public static void main 中......我无法创建一个新的 SimpleCircle。这个错误只发生在我的圈子上。非常感谢你的帮助!:)

public class TestSimpleCircle {

    class SimpleCircle {
        double radius;

        SimpleCircle(){
            radius = 1;
        }

        SimpleCircle(double newRadius){
            radius = newRadius;
        }

        double getArea() {
            return radius * radius * Math.PI;
        }

        double getPerimeter() {
            return 2 * radius * Math.PI;
        }

        void setRadius(double newRadius) {
            radius = newRadius;
        }
    }

    public static void main(String [] args) {
        SimpleCircle circle = new SimpleCircle();
        System.out.println("the area of the circle of radius "+circle.radius+" is "+circle.getArea());

        SimpleCircle circle2 = new SimpleCircle(25);
        System.out.println("the area of the circle of radius "+circle2.radius+" is "+circle2.getArea());

        SimpleCircle circle3 = new SimpleCircle(125);
        System.out.println("the area of the circle of radius "+circle3.radius+" is "+circle3.getArea());

        circle.radius = 100;
        System.out.println("The area of the circle of radius "+circle.radius+" is "+circle.getArea());
    }
}

采纳答案by Ivan

You declared you SimpleCircle class as inner class for TestSimpleCircle. You need either move it into a separate file or declare it as

您将 SimpleCircle 类声明为 TestSimpleCircle 的内部类。您需要将其移动到单独的文件中或将其声明为

static class SimpleCircle

回答by Przemys?aw Moskal

SimpleCircleis an inner class of class TestSimpleCircle. This means that you first need an instance of object of enclosing class, for example:

SimpleCircle是 class 的内部类TestSimpleCircle。这意味着您首先需要一个封闭类的对象实例,例如:

TestSimpleCircle tsc = new TestSimpleCircle();

Now you are able to create an instance of inner class that is connected with an instance of enclosing TestSimpleCircleclass:

现在您可以创建一个与封闭TestSimpleCircle类实例连接的内部类实例:

SimpleCircle sc = tsc.new SimpleCircle();

As you see, to create an instance of object of inner class you had to specify to which exactly object of enclosing class you want it to belong (with the tsc.newin your example).

如您所见,要创建内部类对象的实例,您必须指定您希望它属于哪个封闭类的确切对象(tsc.new在您的示例中使用 )。

If you need to create an instance of SimpleCircle without instance of object of enclosing class you should have declared this class as static class SimpleCircle{code of your class here}

如果你需要创建一个 SimpleCircle 的实例而没有封闭类的对象的实例,你应该将这个类声明为 static class SimpleCircle{code of your class here}