我想编写魔方代码,每个面需要六个Face类。在这个类中,为了正确移动魔方,我需要访问其四周的面,因此我试图在一个Face的构造函数中创建四个其他Face对象。我想知道这种实例化方式是否可行。以下是我的代码:
(第一段代码来自主类,第二段代码来自Face类):
white = new Face(red, blue, green, orange, Color.WHITE);
yellow = new Face(orange, blue, green, red, Color.YELLOW);
red = new Face(yellow, blue, green, white, Color.RED);
orange = new Face(white, blue, green, yellow, Color.ORANGE);
blue = new Face(red, yellow, white, orange, Color.BLUE);
green = new Face(red, white, yellow, orange, Color.GREEN);
front = yellow;
public Face top, left, right, bottom;
public Cell[][] cells;
public Face(Face t, Face l, Face r, Face b, Color c) {
top = t;
left = l;
right = r;
bottom = b;
cells = new Cell[3][3];
for(int row = 0; row < 3; row++) {
for(int col = 0; col < 3; col++) {
cells[row][col] = new Cell(c);
}
}
}