【问题标题】:Must qualify the allocation with an enclosing instance of type GeoLocation必须使用 GeoLocation 类型的封闭实例来限定分配
【发布时间】:2012-03-16 21:17:29
【问题描述】:

我收到此错误 -

无法访问 GeoLocation 类型的封闭实例。必须使用 GeoLocation 类型的封闭实例来限定分配(例如 x.new 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);
        }
    }

}

【问题讨论】:

标签: java


【解决方案1】:

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

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

GeoLocation outer = new GeoLocation();

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

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

【讨论】:

    【解决方案2】:

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

    public static class ThreadTask implements Runnable { ... }
    

    【讨论】:

      【解决方案3】:

      创建内联类static

      public class OuterClass {
      
          static class InnerClass {
          }
      
          public InnerClass instance = new OuterClass.InnerClass();
      }
      

      那么就可以如下实例化内部类:

      new OuterClass.InnerClass();
      

      【讨论】:

        【解决方案4】:

        做这个结构:

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

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

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

            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 类型的封闭实例来限定分配(例如 x.new A(),其中 x 是 StackArrList 的实例)。并且不允许创建 Stack 类的实例

            当您将 class Stack 设置为 static class 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;
            
                    }
            
                }
            
            }
            

            【讨论】:

              猜你喜欢
              • 2012-03-24
              • 2018-05-12
              • 1970-01-01
              • 2014-01-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-02-22
              • 2018-06-03
              相关资源
              最近更新 更多