必须使用GeoLocation类型的封闭实例来限定分配


109

我收到此错误为-

无法访问类型为GeoLocation的封闭实例。必须用一个封闭的GeoLocation类型的实例(例如xxA(),其中x是GeoLocation的实例)来限定分配。新的ThreadTask(i)上将出现此错误。我不知道为什么会这样。任何建议将不胜感激。

public class GeoLocation {
    public static void main(String[] args) throws InterruptedException {
        int size = 10;

        // create thread pool with given size
        ExecutorService service = Executors.newFixedThreadPool(size); 

        // queue some tasks
        for(int i = 0; i < 3 * size; i++) {
            service.submit(new ThreadTask(i));
        }

        // wait for termination        
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
    }

    class ThreadTask implements Runnable {
        private int id;

        public ThreadTask(int id) {
            this.id = id;
        }

        public void run() {
            System.out.println("I am task " + id);
        }
    }

}


Answers:


150

嗨,我找到了一个解决方案;-)

发生此错误的原因是,您尝试创建内部类的实例service.submit(new ThreadTask(i)); 而不创建主类的实例。

要解决此问题,请首先创建主类的实例:

GeoLocation outer = new GeoLocation();

然后创建要调用的类的实例,如下所示:

service.submit(outer.new ThreadTask(i));

我希望这能解决您的问题;-)


101

我更喜欢的另一种选择是将内部类设置为静态。

public static class ThreadTask implements Runnable { ... }

对我来说也很完美。我认为这应该是首选解决方案,因为1)最简单,2)主要方法是静态的。
艾伦(Alan)

15

进行内联类 static

public class OuterClass {

    static class InnerClass {
    }

    public InnerClass instance = new OuterClass.InnerClass();
}

然后可以实例化内部类,如下所示:

new OuterClass.InnerClass();

3

做这个结构:

文件 GeoLocation.java

public class GeoLocation {

    public static void main(String[] args) throws InterruptedException {

        int size = 10;

        // create thread pool with given size
        ExecutorService service = Executors.newFixedThreadPool(size); 

        // queue some tasks
        for(int i = 0; i < 3 * size; i++) {
            service.submit(new ThreadTask(i));
        }

        // wait for termination        
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
    }

}

文件 ThreadTask.java

public class ThreadTask implements Runnable {
    private int id;

    public ThreadTask(int id) {
        this.id = id;
    }

    public void run() {
        System.out.println("I am task " + id);
    }
}

4
欢迎使用SO,在这里,这是一个很好的做法,解释为什么要使用您的解决方案,而不仅仅是解释如何使用。这将使您的答案更有价值,并帮助更多的读者更好地了解您的操作方式。我还建议您查看我们的常见问题解答:stackoverflow.com/faq
ForceMagic 2012年

2

您需要创建父类的实例才能创建内部类的实例。这是一个例子:

package RandomTests;

public class FinalConstructorTest {


    public static void main (String [] arg){
        FinalConstructorTest fct= new FinalConstructorTest();
        InnerClass1 f1= fct.new InnerClass1(99);
        InnerClass2 f2= fct.new InnerClass2();
    }

    class InnerClass1{
        private final int num2;

        protected InnerClass1(int num){
            num2= num;
            System.out.println("num2= "+ num2);
        }
    }
    class InnerClass2{
        //private static final int x; //Doesn't work
        private final int y; 

        {
            y= 5;
            System.out.println("y= "+ y);
        }
    }
}

1

如果您要通过静态方法或类似方式访问非静态成员,也可能会发生这种情况。以下是两个不同的方面,一个导致错误,另一个解决了代码段。这只是使其他类为“静态”的问题

package Stack;

import java.util.Stack;
import java.util.*;

public class StackArrList {

    public static void main(String[] args) {


        Scanner in = new Scanner(System.in);

        Stack S = new Stack();
        System.out.println("Enter some integers and keep 0 at last:\n");
        int n = in.nextInt();

        while (n != 0) {
            S.push(n);
            n = in.nextInt();
        }
        System.out.println("Numbers in reverse order:\n");

        while (!S.empty()) {

            System.out.printf("%d", S.pop());
            System.out.println("\n");

        }

    }

    public class Stack {
        final static int MaxStack = 100;
        final static int Value = -999999;
        int top = -1;
        int[] ST = new int[MaxStack];

        public boolean empty() {
            return top == -1;
        }

        public int pop() {

            if (this.empty()) {
                return Value;
            }
            int hold = ST[top];
            top--;
            return hold;
        }

        public void push(int n) {
            if (top == MaxStack - 1) {
                System.out.println("\n Stack Overflow\n");
                System.exit(1);
            }
            top++;
            ST[top] = n;

        }

    }

}

引发错误没有可访问类型StackArrList的封闭实例。必须使用类型为StackArrList的封闭实例(例如xxnew A(),其中x是StackArrList的实例)对分配进行限定。并且不允许创建Stack类的实例

当您将类Stack设置静态时,Stack可以正常工作,并且不会出现任何错误。

package Stack;

import java.util.Stack;
import java.util.*;

public class StackArrList {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        Stack S = new Stack();
        System.out.println("Enter some integers and keep 0 at last:\n");
        int n = in.nextInt();

        while (n != 0) {
            S.push(n);
            n = in.nextInt();
        }
        System.out.println("Numbers in reverse order:\n");

        while (!S.empty()) {

            System.out.printf("%d", S.pop());
            System.out.println("\n");

        }

    }

    static class Stack {
        final static int MaxStack = 100;
        final static int Value = -999999;
        int top = -1;
        int[] ST = new int[MaxStack];

        public boolean empty() {
            return top == -1;
        }

        public int pop() {

            if (this.empty()) {
                return Value;
            }
            int hold = ST[top];
            top--;
            return hold;
        }

        public void push(int n) {
            if (top == MaxStack - 1) {
                System.out.println("\n Stack Overflow\n");
                System.exit(1);
            }
            top++;
            ST[top] = n;

        }

    }

}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.