开发者

Object Literals in Java

开发者 https://www.devze.com 2023-03-19 19:50 出处:网络
I\'m converting a Javascript program I wrote into Java, and there\'s one Object which I\'m not sure how to write in Java.

I'm converting a Javascript program I wrote into Java, and there's one Object which I'm 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;

Thats about 1/4 of the whole object, but the rest is more of the same.

How can I write this in Java?


I would start at the deepest level and work your way backwards from that, thinking about how you can convert each set of properties into instance variables of a class, e.g.:

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

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

Then you can put these into classes. From your code these seem to be properties of a rotation, so I'd suggest something like:

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
}

Then something like:

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

And so on. It seems like you're really new to some formal object-oriented concepts though (e.g. encapsulation), so I'd recommend reading up on some Java tutorials that cover the basics of classes and objects before you attempt to port this to Java.


Java and Javascript are two completelly different languages. It may not be possible to find exact mapping. You can write things in much more relaxed way in Javascript, so when switching to Java you have to think what every object is and what property to use.

In your case, for example, types is an array and rotations is an array inside element of types. You would have to initialize them to arrays. For that you will need to know size in advance. You may do something like this:

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

etc...

That's provided that tetros has field types and TypeObject has field rotations.

Generally, it's not good idea to use fields too much in Java. Fields with setter and getter are more favored from style standpoint.

So it may feel very cumbersome to you to do certain things in Java and most likely new data design will be required.

Overall, porting from one language to another is a task that requires in depth knowledge of both languages. So in you case, you may consider writing it from scratch in Java while using original Javascript source as a guideline.


I would honestly start by going back and rewriting your Javascript version to use prototypes instead of ad-hoc objects. Then the mapping between prototypes and Java classes would be much more 1-to-1. As is, I don't know if you can do much since Java doesn't have the concept of Object literals, and Java doesn't really support building up objects dynamically like this.

0

精彩评论

暂无评论...
验证码 换一张
取 消