Java 没有封闭的类型实例...在范围内

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

no enclosing instance of type... in scope

javainheritanceinstantiationinner-classes

提问by gstackoverflow

I investigate java inner classes.

我研究了 Java 内部类。

I wrote example:

我写的例子:

public class Outer {
    public Outer(int a){}

    public class Inner {
        public Inner(String str, Boolean b){}
    }

    public static class Nested extends Inner{
        public static void m(){
            System.out.println("hello");
        }
        public Nested(String str, Boolean b , Number nm)   { super("2",true);   }
    }

    public class InnerTest extends Nested{
        public InnerTest(){  super("str",true,12);  }
    }
}

I invoke it from main using following string:

我使用以下字符串从 main 调用它:

 new Outer(1).new Inner("",true);

I see compile error:

我看到编译错误:

  java: no enclosing instance of type testInheritancefromInner.Outer is in scope

Can you explain me this situation?

你能解释一下这种情况吗?

UPDATE

更新

enter image description here

在此处输入图片说明

采纳答案by Sotirios Delimanolis

Inneris an inner class. It can only be created when there is an enclosing instance of the class containing the Innerclass definition.

Inner是一个内部类。只有当存在包含Inner类定义的类的封闭实例时才能创建它。

However, you've created a staticnested class, Nested, which extends from this class. When you try to invoke the super constructor

但是,您已经创建了一个static嵌套类Nested,它从这个类扩展而来。当您尝试调用超级构造函数时

public Nested(String str, Boolean b , Number nm)   { super("2",true);   }

it will fail because the super constructor, for Inner, depends on an instance of Outer, which doesn't exist in the staticcontext of the Nestedclass. Jon Skeet provides a solution.

它将失败,因为超级构造函数 forInner依赖于 的实例Outer,该实例staticNested类的上下文中不存在。Jon Skeet 提供了一个解决方案。

An explanation of the solution appears in the JLS here.

该解决方案的说明出现在JLS这里

Superclass constructor invocations may be subdivided:

  • Unqualified superclass constructor invocations begin with the keyword super (possibly prefaced with explicit type arguments).

  • Qualified superclass constructor invocations begin with a Primary expression.

    • They allow a subclass constructor to explicitly specify the newly created object's immediately enclosing instance with respect to the direct superclass (§8.1.3). This may be necessary when the superclass is an inner class.

超类构造函数调用可以细分:

  • 未限定的超类构造函数调用以关键字 super 开头(可能以显式类型参数开头)。

  • 限定的超类构造函数调用以 Primary 表达式开始。

    • 它们允许子类构造函数针对直接超类(第 8.1.3 节)显式指定新创建的对象的直接封闭实例。当超类是内部类时,这可能是必要的。

回答by Jon Skeet

As Sotirios has said, your nested (not-inner) class doesn't implicitlyhave an instance of Outerto effectively provide to the Inner.

正如 Sotirios 所说,您的嵌套(非内部)类没有隐式地具有一个实例Outer来有效地提供给Inner.

You canget round this, however, by explicitlyspecifying it before the .superpart:

但是,您可以通过在部分之前明确指定它来解决这个问题.super

public Nested(String str, Boolean b, Number nm) { 
    new Outer(10).super("2", true);
}

Or even accept it as a parameter:

或者甚至接受它作为参数:

public Nested(Outer outer) { 
    outer.super("2", true);
}

However, I would stronglyadvise you to avoid such convoluted code. I avoid nested classes most of the time, named inner classes almost always, and I can't ever remember using a combinationof them like this.

但是,我强烈建议您避免使用此类复杂的代码。我大部分时间都避免嵌套类,几乎总是命名内部类,而且我不记得像这样使用它们的组合