我正在将我编写的Javascript程序转换为Java,并且有一个Object我不知道如何用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;
约占整个物体的1/4,但其余部分则相同。
我怎么用Java写这个?
答案 0 :(得分:3)
我会从最深层开始,从那里开始向后工作,考虑如何将每组属性转换为类的实例变量,例如:
int[][] blocks;
int[][] edges;
(请注意,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
}
然后像:
class Type {
private Rotation[] rotations;
private String color;
// etc etc
}
等等。虽然你似乎对某些正式的面向对象概念不熟悉(例如封装),所以我建议你在尝试将它移植到Java之前阅读一些涵盖类和对象基础知识的Java教程。 / p>
答案 1 :(得分:1)
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中使用字段不是很好。具有setter和getter的字段从样式角度来看更受青睐。
因此,在Java中执行某些操作可能会非常麻烦,并且很可能需要进行新的数据设计。
总的来说,从一种语言移植到另一种语言是一项需要深入了解这两种语言的任务。因此,在您的情况下,您可以考虑使用原始Javascript源作为指导原则从头开始编写Java。
答案 2 :(得分:0)
老实说,我会回过头来重新编写您的Javascript版本,以使用原型而不是ad-hoc对象。然后原型和Java类之间的映射将更加一对一。因为我不知道你是否可以做很多事情,因为Java没有Object文字的概念,Java也不支持像这样动态构建对象。