不是一个封闭的Java类 [英] Is not an enclosing class Java

查看:121
本文介绍了不是一个封闭的Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个俄罗斯方块游戏,当我尝试创建一个对象时,我得到形状不是一个封闭的类

I'm trying to make a tetris game and I'm getting "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 Shape.ZShape();
    }
}

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

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

public class Shape {

    private String shape;
    private int[][] coords;
    private int[][] noShapeCoords = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
    private int[][] zShapeCoords = { { 0, -1 }, { 0, 0 }, { -1, 0 }, { -1, 1 } };
    private int[][] sShapeCoords = { { 0, -1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } };
    private int[][] lineShapeCoords = { { 0, -1 }, { 0, 0 }, { 0, 1 }, { 0, 2 } };
    private int[][] tShapeCoords = { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 } };
    private int[][] squareShapeCoords = { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } };
    private int[][] lShapeCoords = { { -1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } };
    private int[][] mirroredLShapeCoords = { { 1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } };

    public Shape(){
        int[][] coords =  noShapeCoords;
        shape = "NoShape";
    }

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

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

 //etc
}

我做错了什么?

推荐答案

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

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

最简单的解决方案是使ZShape和任何嵌套类 static 如果你可以。

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

我还会创建任何字段 final static final 你也可以。

I would also make any fields final or static final that you can as well.

这篇关于不是一个封闭的Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆