Java 无法访问类型测试的封闭实例。必须在简单的测试程序上使用类型测试错误的封闭实例来限定分配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19141792/
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 14:28:18  来源:igfitidea点击:

No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing instance of type test error on a simple test Program

java

提问by user133466

I got the No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing instance of type test error with Location ob1 = new Location(10.0, 20.0);I'm not sure why..

我得到了类型测试的无封闭实例是可访问的。必须使用类型测试错误的封闭实例来限定分配,Location ob1 = new Location(10.0, 20.0);我不知道为什么..

package pkg;

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Location ob1 = new Location(10.0, 20.0);
        Location ob2 = new Location(5.0, 30.0);
        ob1.show();
        ob2.show();
        ob1 = ob1.plus(ob2);
        ob1.show();
        return;
    }

    public class Location // an ADT
    {
        private double longitude, latitude;

        public Location(double lg, double lt) {
            longitude = lg;
            latitude = lt;
        }

        public void show() {
            System.out.println(longitude + " " + latitude);
        }

        public Location plus(Location op2) {
            Location temp = new Location(0.0, 0.0);
            temp.longitude = op2.longitude + this.longitude;
            temp.latitude = op2.latitude + this.latitude;
            return temp;
        }
    }
}

采纳答案by Jops

You may consider splitting them into 2 files. It appears that your intention is not to create nested classes, but rather having a tester class calling your core class.

您可以考虑将它们分成 2 个文件。看来您的意图不是创建嵌套类,而是让测试类调用您的核心类。

File #1: Test.java

文件 #1:Test.java

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Location ob1 = new Location(10.0, 20.0);
        Location ob2 = new Location(5.0, 30.0);
        ob1.show();
        ob2.show();
        ob1 = ob1.plus(ob2);
        ob1.show();
        return;
    }
 }

File #2: Location.java

文件 #2:Location.java

public class Location // an ADT
{
    private double longitude, latitude;

    public Location(double lg, double lt) {
        longitude = lg;
        latitude = lt;
    }

    public void show() {
        System.out.println(longitude + " " + latitude);
    }

    public Location plus(Location op2) {
        Location temp = new Location(0.0, 0.0);
        temp.longitude = op2.longitude + this.longitude;
        temp.latitude = op2.latitude + this.latitude;
        return temp;
    }
}

When you have multiple classes defined inside a single java file, you end up creating dependencies between them, thus you're getting the error "enclosing instance of type". In your code, Testis enclosing Location. These are nested classesand unless you have good design reasons to write your classes that way, it's still best to stick to the 1-file to 1-class approach.

当您在单个 java 文件中定义了多个类时,您最终会在它们之间创建依赖关系,因此您会收到错误“封闭类型实例”。在您的代码中,Test包含Location。这些是嵌套类,除非您有很好的设计理由以这种方式编写类,否则最好仍然坚持使用 1-file to 1-class 方法。

回答by upog

try

尝试

Location ob1 = new test().new Location(10.0, 20.0);
Location ob2 = new test().new Location(5.0, 30.0);

you need to create an instance of outer class first, then you can create an instance of inner class

您需要先创建外部类的实例,然后才能创建内部类的实例

回答by Aditya Sinha

I am very new to java, but I found the solution to this problem. The thing is wrong enclosion of braces.

我对java很陌生,但我找到了解决这个问题的方法。事情是错误的括号括起来。

package pkg;

public class test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Location ob1 = new Location(10.0, 20.0);
    Location ob2 = new Location(5.0, 30.0);
    ob1.show();
    ob2.show();
    ob1 = ob1.plus(ob2);
    ob1.show();
    return;
}
}

public class Location // an ADT
{
    private double longitude, latitude;

    public Location(double lg, double lt) {
        longitude = lg;
        latitude = lt;
    }

    public void show() {
        System.out.println(longitude + " " + latitude);
    }

    public Location plus(Location op2) {
        Location temp = new Location(0.0, 0.0);
        temp.longitude = op2.longitude + this.longitude;
        temp.latitude = op2.latitude + this.latitude;
        return temp;
    }
}

回答by Combine

There are several solutions you can do to fix the error:

您可以采取多种解决方案来修复错误:

  1. As already mentioned - make the two classes in separate files

  2. Make your inner class also static

        public static class Location { ... }
    
  3. Assuming this is only for test/learn purposes and you want to fix the error - you can call the 'Location' class like that. Mentioning this case because this illustrates the root of the error - if the class is not-static like in the original question - you have to make an object from the parent class in order to access the inner class. However fix 1,2 are better unless you have a special reason write it this way.

    Location ob1 = new test().new Location(10.0, 20.0);
    Location ob2 = new test().new Location(5.0, 30.0);
    
  1. 如前所述 - 在单独的文件中创建两个类

  2. 使您的内部类也是静态的

        public static class Location { ... }
    
  3. 假设这仅用于测试/学习目的,并且您想修复错误 - 您可以像这样调用“位置”类。提到这种情况是因为这说明了错误的根源-如果类不是原始问题中的静态类-您必须从父类创建一个对象才能访问内部类。但是,除非有特殊原因,否则修复 1,2 会更好。

    Location ob1 = new test().new Location(10.0, 20.0);
    Location ob2 = new test().new Location(5.0, 30.0);