【问题标题】:Java: How to return object when I have its name as a string?Java:当我将其名称作为字符串时如何返回对象?
【发布时间】:2018-09-14 21:28:52
【问题描述】:

假设我正在构建一个国际象棋游戏并创建棋盘空间对象。我正在创建这样的对象,棋盘上的所有空间正好是 64:

BoardSpace a1 = new BoardSpace("black", 1, 1, true);

这是我为 BoardSpace 对象创建的类:

public class BoardSpace {
    String color;
    int x_pos;
    int y_pos;
    boolean occupied;

    //constructor
    public BoardSpace (String color, int x_pos, int y_pos, boolean occupied) { 
        this.color = color;
        this.x_pos = x_pos; 
        this.y_pos = y_pos; 
        this.occupied = occupied;
    } 
}

在棋盘上移动棋子之前,我创建了所有 BoardSpace 对象。我的棋子对象每个都有一个 x 位置和 y 位置。我要做的是将它们的坐标转换为 BoardPiece 名称,然后从该名称中检索先前创建的 BoardPiece 对象。

这就是我想做的:

static String get_BoardSpace_color(int x_pos, int y_pos){
    int modified_x = x_pos + 96; //adjusting for ASCII
    char c = (char)(modified_x);
    String space_name = ""+c+y_pos;

    BoardSpace piece = (BoardSpace)(space_name); //PROBLEM AREA

    return piece.color;
}

我怎样才能使用已经存在的对象名称的正确字符串表示来实际检索该对象?

【问题讨论】:

  • 对象没有名字。为什么不创建 BoardSpace 的 8x8 2D 数组,例如称为 grid 并简单地获取 grid[x_pos][y_pos] 对象?
  • 我建议您使用 enum 而不是 string 作为颜色变量
  • 您无权访问变量的名称。与其尝试通过变量名访问东西,不如将实例存储在某个集合中。像List 或者可能是一个数组。然后在那里访问它们,例如list.get(4)array[4]

标签: java string class object


【解决方案1】:

同样,对象没有名字。是的,变量可以,但是变量的名称不是字符串,并且编译代码中几乎不存在变量名称。您需要一种方法来获得对感兴趣对象的引用,并且有多种方法可以做到这一点,包括:

  • Map<String, BoardSpace>,例如HashMap<String, BoardSpace>。通过这种方式,您可以将字符串与唯一对象相关联
  • 一个集合,例如 ArrayList<BoardSpace>,它允许您通过 int 索引获取对象
  • 一个简单的BoardSpace数组,如BoardSpace[64]
  • 二维嵌套集合,例如List<List<BoardSpace>>
  • 或二维数组。

由于您似乎正在制作一个 8 x 8 的 BoardSpace 网格,并且这些尺寸可能不会改变,因此这里最简单的是创建一个 8x8 对象数组:

private BoardSpace[][] grid = new BoardSpace[8][8];

然后您可以使用您的 x 和 y(或行和列)索引来获取感兴趣的对象。

例如:

public class TestBoardSpace {
    public static void main(String[] args) {
        Board board = new Board();
        for (int y = 0; y < Board.ROWS; y++) {
            for (int x = 0; x < Board.COLS; x++) {
                System.out.printf("%8s ", board.getBoardSpace(x, y).getColor());
            }
            System.out.println();
        }
    }
}

class Board {
    public static final int ROWS = 8;
    public static final int COLS = ROWS;
    private BoardSpace[][] grid = new BoardSpace[ROWS][COLS];

    public Board() {
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                MyColor color = row % 2 == col % 2 ? MyColor.BLACK : MyColor.WHITE;
                grid[row][col] = new BoardSpace(color, col, row, false);
            }
        }
    }

    public BoardSpace getBoardSpace(int x, int y) {
        // to get color, simply call getColor() on this
        return grid[y][x];
    }

}

// yes an enum here would be great and would protect against 
// bad Strings
enum MyColor {
    WHITE, BLACK
}

class BoardSpace {
    private MyColor color;
    private int x_pos;
    private int y_pos;
    private boolean occupied;

    // constructor
    public BoardSpace(MyColor color, int x_pos, int y_pos, boolean occupied) {
        this.color = color;
        this.x_pos = x_pos;
        this.y_pos = y_pos;
        this.occupied = occupied;
    }

    public boolean isOccupied() {
        return occupied;
    }

    public void setOccupied(boolean occupied) {
        this.occupied = occupied;
    }

    public MyColor getColor() {
        return color;
    }

    public int getX_pos() {
        return x_pos;
    }

    public int getY_pos() {
        return y_pos;
    }

}

【讨论】:

  • 非常感谢!
  • @Lily:不客气。这是国际象棋或跳棋程序吗?如果是这样,您可能希望创建将等级和文件字符转换为适当的数组索引的方法,反之亦然。
猜你喜欢
  • 1970-01-01
  • 2013-12-31
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 1970-01-01
相关资源
最近更新 更多