不能访问类型测试的封闭实例。必须在简单测试程序上使用封闭的类型测试错误实例来限定分配 [英] 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

查看:409
本文介绍了不能访问类型测试的封闭实例。必须在简单测试程序上使用封闭的类型测试错误实例来限定分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了无法封闭的类型测试实例。必须使用位置ob1 =新位置(10.0,20.0); 我不知道为什么......

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..

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;
        }
    }
}


推荐答案

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

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.

文件#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;
    }
 }

文件#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;
    }
}

当你在一个java文件中定义了多个类时,你最终创建它们之间的依赖关系,因此你得到错误封闭类型的实例。在您的代码中,测试包含位置。这些是嵌套类,除非你有很好的设计理由要写这样你的课程,最好坚持1档到1道的方法。

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, Test is enclosing Location. These are nested classes and 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.

这篇关于不能访问类型测试的封闭实例。必须在简单测试程序上使用封闭的类型测试错误实例来限定分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆