Java中的对象文字

Die*_*l11 5 java arrays object

我正在将我编写的Javascript程序转换为Java,并且有一个对象,我不确定如何用Java编写.

    tetros.types = new Array();
    tetros.types[0] = new Object();

    tetros.types[0].color = "green";
    tetros.types[0].rotations = new Array();
    tetros.types[0].rotations[0] = new Object();
    tetros.types[0].rotations[0].blocks = Array([0,0],[1,0],[2,0],[2,1]);
    tetros.types[0].rotations[0].edges ={
        "down"  :Array([2,0],[2,1]),
        "left"  :Array([0,0],[1,0],[2,0]),
        "right" :Array([2,1],[1,0],[0,0]),
        "up"    :Array([0,0],[2,1])
    }

    tetros.types[0].rotations[1] = new Object();
    tetros.types[0].rotations[1].blocks = Array([0,0],[0,1],[0,2],[1,0]);
    tetros.types[0].rotations[1].edges ={
        "down"  :Array([0,1],[0,2],[1,0]),
        "left"  :Array([0,0],[1,0]),
        "right" :Array([0,2],[1,0]),
        "up"    :Array([0,0],[1,0])
    }

    tetros.types[0].rotations[2] = new Object();
    tetros.types[0].rotations[2].blocks = Array([0,0],[0,1],[1,1],[2,1]);
    tetros.types[0].rotations[2].edges ={
        "down"  :Array([0,0],[2,1]),
        "left"  :Array([0,0],[1,1],[2,1]),
        "right" :Array([0,1],[1,1],[2,1]),
        "up"    :Array([0,0],[0,1])
    }

    tetros.types[0].rotations[3] = new Object();
    tetros.types[0].rotations[3].blocks = Array([0,2],[1,0],[1,1],[1,2]);
    tetros.types[0].rotations[3].edges ={
        "down"  :Array([1,0],[1,1],[1,2]),
        "left"  :Array([1,0],[0,2]),
        "right" :Array([0,2],[1,2]),
        "up"    :Array([0,2],[1,0],[1,1])
    }


    tetros.types[1] = new Object();
    tetros.types[1].color = "blue";
    tetros.types[1].rotations = new Array();
    tetros.types[1].rotations[0] = new Object();
    tetros.types[1].rotations[0].blocks = Array([0,0],[0,1],[1,0],[1,1]);
    tetros.types[1].rotations[0].edges ={
        "down"  :Array([1,0],[1,1]),
        "left"  :Array([0,0],[1,0]),
        "right" :Array([0,1],[1,1]),
        "up"    :Array([0,0],[0,1])
    }
    tetros.types[1].rotations[1] = new Object();
    tetros.types[1].rotations[1].blocks = tetros.types[1].rotations[0].blocks;
    tetros.types[1].rotations[1].edges = tetros.types[1].rotations[0].edges;

    tetros.types[1].rotations[2] = new Object();
    tetros.types[1].rotations[2].blocks = tetros.types[1].rotations[0].blocks;
    tetros.types[1].rotations[2].edges = tetros.types[1].rotations[0].edges;

    tetros.types[1].rotations[3] = new Object();
    tetros.types[1].rotations[3].blocks = tetros.types[1].rotations[0].blocks;
    tetros.types[1].rotations[3].edges = tetros.types[1].rotations[0].edges;
Run Code Online (Sandbox Code Playgroud)

这大约占整个物体的1/4,但其余部分则相同.

我怎么能用Java写这个呢?

小智 3

我将从最深的层次开始,然后从头开始,思考如何将每组属性转换为类的实例变量,例如:

int[][] blocks;
int[][] edges;
Run Code Online (Sandbox Code Playgroud)

(请注意,Java 不支持关联数组,如“向下”、“向上”等)。

然后你可以将它们放入类中。从您的代码来看,这些似乎是旋转的属性,所以我建议如下:

class Rotation {
    // Instance variables
    private int[][] blocks;
    private int[][] edges;

    // Constructor
    public Rotation(int[][] blocks, int[][] edges) {
        this.blocks = blocks;
        this.edges = edges;
    }

    // Accessors and mutators here if applicable
}
Run Code Online (Sandbox Code Playgroud)

然后是这样的:

class Type {
    private Rotation[] rotations;
    private String color;
    // etc etc
}
Run Code Online (Sandbox Code Playgroud)

等等。看起来您对一些正式的面向对象概念(例如封装)确实很陌生,所以我建议您在尝试将其移植到 Java 之前阅读一些涵盖类和对象基础知识的 Java 教程。