Java object literals

I am converting a Javascript program that I wrote in Java, and there is one object there that I am not sure how to write in 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;

This is about 1/4 of the whole object, but everything else is something more.

How can I write this in Java?

+5
source share
3 answers

I would start at the deepest level and lean back on this, thinking about how you can convert each set of properties into class instance variables, for example:

int[][] blocks;
int[][] edges;

(note that Java does not support associative arrays for things like "down", "up", etc.).

. , - :

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
}

- :

class Type {
    private Rotation[] rotations;
    private String color;
    // etc etc
}

. , - (, ), Java, , Java.

+3

Java Javascript - . . Javascript, Java , .

, , types - , rotations - types. . . - :

tetros.types = new TypeObject[10];
tetros.types[0] = new TypeObject();
tetros.types[0].rotations = new Rotation[10];
tetros.types[0].rotations[3] = new Rotation();

....

, tetros types TypeObject rotations.

, Java. .

, Java , , .

, - , . , , Java, Javascript .

+1

I would honestly start by going back and rewriting your version of Javascript to use prototypes instead of special objects. Then the comparison between prototypes and Java classes will be much more from 1 to 1. Like me, I don’t know if you can do a lot, since Java does not have the concept of Object literals, and Java really does not support the dynamic creation of objects in this way.

0
source

All Articles