不是封闭类 Java

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

Is not an enclosing class Java

javainner-classes

提问by V Sebi

I'm trying to make a Tetris game and I'm getting the compiler error

我正在尝试制作俄罗斯方块游戏,但出现编译器错误

Shape is not an enclosing class

Shape is not an enclosing class

when I try to create an object

当我尝试创建一个对象时

public class Test {
    public static void main(String[] args) {
        Shape s = new Shapes.ZShape();
    }
}

I'm using inner classes for each shape. Here's part of my code

我为每个形状使用内部类。这是我的代码的一部分

public class Shapes {
    class AShape {
    }
    class ZShape {
    }
}

What am I doing wrong ?

我究竟做错了什么 ?

采纳答案by Peter Lawrey

ZShapeis not static so it requires an instance of the outer class.

ZShape不是静态的,所以它需要一个外部类的实例。

The simplest solution is to make ZShape and any nested class staticif you can.

如果可以,最简单的解决方案是创建 ZShape 和任何嵌套类static

I would also make any fields finalor static finalthat you can as well.

我也会做任何领域,final或者static final你也可以。

回答by Peter Lawrey

I have encountered the same problem. I solved by creating an instance for every inner public Class. as for you situation, i suggest you use inheritance other than inner classes.

我遇到了同样的问题。我通过为每个内部公共类创建一个实例来解决。至于你的情况,我建议你使用内部类以外的继承。

public class Shape {

    private String shape;

    public ZShape zShpae;
    public SShape sShape;

    public Shape(){
      int[][] coords =  noShapeCoords;
      shape = "NoShape";
      zShape = new ZShape();
      sShape = new SShape();
    }

    class ZShape{
      int[][] coords =  zShapeCoords;
      String shape = "ZShape";
    }

    class SShape{
      int[][] coords = sShapeCoords;
      String shape = "SShape";
    }

 //etc
}

then you can new Shape(); and visit ZShape through shape.zShape;

然后你可以 new Shape(); 并通过 shape.zShape 访问 ZShape;

回答by Vishal Kumar

Suppose RetailerProfileModel is your Main class and RetailerPaymentModel is an inner class within it. You can create an object of the Inner class outside the class as follows:

假设 RetailerProfileModel 是您的 Main 类,而 RetailerPaymentModel 是其中的一个内部类。您可以在类外创建 Inner 类的对象,如下所示:

RetailerProfileModel.RetailerPaymentModel paymentModel
        = new RetailerProfileModel().new RetailerPaymentModel();

回答by Younes

No need to make the nested class as static but it must be public

无需将嵌套类设为静态,但它必须是公共的

public class Test {
    public static void main(String[] args) {
        Shape shape = new Shape();
        Shape s = shape.new Shape.ZShape();
    }
}

回答by Amit Upadhyay

What I would suggest is not converting the non-static class to a static class because in that case, your inner class can't access the non-static members of outer class.

我建议不要将非静态类转换为静态类,因为在这种情况下,您的内部类无法访问外部类的非静态成员。

Example :

例子 :

class Outer
{
    class Inner
    {
        //...
    }
}

So, in such case, you can do something like:

因此,在这种情况下,您可以执行以下操作:

Outer o = new Outer();
Outer.Inner obj = o.new Inner();

回答by CoDe

In case if Parent class is singleton use following way:

如果父类是单例,请使用以下方式:

Parent.Child childObject = (Parent.getInstance()).new Child();

where getInstance()will return parent class singleton object.

哪里getInstance()将返回父类单例对象。

回答by Suragch

One thing I didn't realize at first when reading the accepted answer was that making an inner class static is basically the same thing as moving it to its own separate class.

在阅读已接受的答案时,我起初没有意识到的一件事是,使内部类静态与将其移动到自己的单独类基本相同。

Thus, when getting the error

因此,当得到错误

xxx is not an enclosing class

xxx 不是封闭类

You can solve it in either of the following ways:

您可以通过以下任一方式解决:

  • Add the statickeyword to the inner class, or
  • Move it out to its own separate class.
  • static关键字添加到内部类,或
  • 将它移到它自己单独的类中。

回答by Brennan Miller

As stated in the docs:

文档中所述:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

回答by M9J_cfALt

Sometimes, we need to create a new instance of an inner class that can't be static because it depends on some global variables of the parent class. In that situation, if you try to create the instance of an inner class that is not static, a not an enclosing classerror is thrown.

有时,我们需要创建一个内部类的新实例,它不能是静态的,因为它依赖于父类的一些全局变量。在这种情况下,如果您尝试创建非静态内部类的实例,not an enclosing class则会引发错误。

Taking the example of the question, what if ZShapecan't be static because it need global variable of Shapeclass?

以问题为例,如果ZShape不能是静态的,因为它需要Shape类的全局变量怎么办?

How can you create new instance of ZShape? This is how:

你怎么能创建新的实例ZShape?这是如何:

Add a getter in the parent class:

在父类中添加一个getter:

public ZShape getNewZShape() {
    return new ZShape();
}

Access it like this:

像这样访问它:

Shape ss = new Shape();
ZShape s = ss.getNewZShape();

回答by Антон Лялин

Shape shape = new Shape();
Shape.ZShape zshape = shape.new ZShape();