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

时间:2013-10-02 16:19:53

标签: java

我得到了无法封闭的类型测试实例。必须使用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;
        }
    }
}

4 个答案:

答案 0 :(得分:7)

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

首先需要创建外部类的实例,然后可以创建内部类的实例

答案 1 :(得分:3)

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

文件#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文件中定义了多个类,则最终会在它们之间创建依赖关系,因此您将收到错误“封装类型实例”。在您的代码中,测试包含位置。这些是nested classes,除非你有充分的设计理由以这种方式编写类,否则最好坚持使用1文件到1类的方法。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您可以通过多种解决方案来纠正错误:

  1. 如前所述-将两个类放在单独的文件中

  2. 使您的内部类也静态

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

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