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

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

Must qualify the allocation with an enclosing instance of type GeoLocation

java

提问by arsenal

I am getting this error as-

我收到此错误 -

No enclosing instance of type GeoLocation is accessible. Must qualify the allocation with an enclosing instance of type GeoLocation (e.g. x.new A() where x is an instance of GeoLocation).This error is coming on new ThreadTask(i). I don't know why is it happening. Any suggestions will be appreciated.

无法访问 GeoLocation 类型的封闭实例。必须使用 GeoLocation 类型的封闭实例限定分配(例如 xnew A(),其中 x 是 GeoLocation 的实例)。此错误发生在new 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);
        }
    }

}

采纳答案by user1528582

Hi I found a solution for this ;-)

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

This error happens because you're trying to create an instance of an inner class service.submit(new ThreadTask(i));without creating instance of main class..

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

To resolve this issue please create instance of main class first:

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

GeoLocation outer = new GeoLocation();

Then create instance of class you intended to call, as follows:

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

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

I hope this will resolve your issue;-)

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

回答by DanO

Another option, and the one I prefer, would be to set the inner class to be static.

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

public static class ThreadTask implements Runnable { ... }

回答by Rafael Pereira

Do this Structure:

做这个结构:

FILE GeoLocation.java

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

}

}

File ThreadTask.java

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

回答by kangear

Make the inline class static.

制作内联类 static

public class OuterClass {

    static class InnerClass {
    }

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

Then you can instantiate the inner class as follows:

然后,您可以按如下方式实例化内部类:

new OuterClass.InnerClass();

回答by Gaurav Karia

This may happen too if you are accessing non-static members from the static methods or likewise. Following are two different aspects, one which cause the error and other solved piece of code. it's just the matter of making other as class "static"

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

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;

        }

    }

}

This throws the error No enclosing instance of type StackArrList is accessible. Must qualify the allocation with an enclosing instance of type StackArrList (e.g. x.new A() where x is an instance of StackArrList).and will not allow to make instance of Stack class

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

When you make the class Stackto static class Stackwill work fine and no error will be there.

当您将类 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 Gene

You need to create an instance of the parent class in order to create instances of your inner classes. Here is an example:

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

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