Java:对象的封装和数组

Java: Encapsulation and array of objects

我正在 Java 中制作井字游戏来复习一些 Java 概念并练习 OOP。到目前为止,我有 4 个 classes:BoardPlayerCoordinateGame。我在测试 Board class 时遇到问题。棋盘由坐标数组组成。 Board 目前有两个不同的构造函数。采用单个 Coordinate 的构造函数主要用于测试目的。

Board.java

public class Board {

    private Coordinate squares;
    private Coordinate[] coordinateSet;

    public Board(Coordinate squares) {
        this.squares = squares;
    }

    public Board(Coordinate[] coordinateSet) {
        this.coordinateSet = coordinateSet;
    }

    public Coordinate getSquares() {
        return squares;
    }

    public Coordinate[] getCoordinates() {
        return coordinateSet;
    }

    public Coordinate getCoordinate(int i) {
        return coordinateSet[i];
    }
}

Coordinate.java

public class Coordinate {

    private int x;
    private int y;
    private int xFlag; // 0 if available, 1 if not available
    private int yFlag; // 0 if available, 1 if not available
    private boolean squareAvailability;

    public Coordinate(int x, int y, int xFlag, int yFlag) {
        this.x = x;
        this.y = y;
        this.xFlag = xFlag;
        this.yFlag = yFlag;
    }

    public int getXCoordinate() {
        return x;
    }

    public int getYCoordinate() {
        return y;
    }

    public int getXFlag() {
        return xFlag;
    }

    public int getYFlag() {
        return yFlag;
    }

    public void setXFlag() {
        if(xFlag == 0) {
            xFlag = 1;
        } else { }
    }

    public void setYFlag() {
        if(yFlag == 0) {
            yFlag = 1;
        } else { }
    }

    public void add(Coordinate coordinate) {
        // TODO Auto-generated method stub

    }

    public boolean isSquareAvailable(Coordinate coordinate) {
        if(coordinate.getXFlag() == 0) {
            if(coordinate.getYFlag() == 0) {
                squareAvailability = true;
            } else {
                squareAvailability = false;
            }
        } else {
            squareAvailability = false;
        }       

        return squareAvailability;
    }

}

BoardTest.java

import edu.learning.tictactoe.Board;
import edu.learning.tictactoe.Coordinate;

public class BoardTest {

    public static void main(String[] args) {
        Coordinate[] coordinates = new Coordinate[9];   
        Coordinate coordinate = new Coordinate(0, 0, 0, 0);
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                coordinates[i + j] = new Coordinate(i, j, 0, 0);
            }
        }
        Board board = new Board(coordinate);
        System.out.println(board.getSquares().getXCoordinate());
        Board board2 = new Board(coordinates);
        System.out.println(board2.getCoordinates().getCoordinate(0).getXCoordinate()); // error
    }
}

Cannot invoke getCoordinate(int) on the array type Coordinate[] 在 IDE 的 BoardTest.java 的最后一行出现错误。问题是我正在尝试从 Coordinate 对象数组中 return 一个 Coordinate 对象,这是不可能的(?)那么我如何 return a Coordinate 来自 Coordinates?

的数组

board.getCoordinates() returns 一个数组,编译器错误告诉你这个数组没有方法 getCoordinate(int)

直接写

board.getCoordinate(0).getXCoordinate()

如果你想通过你可以写的数组访问坐标

board.getCoordinates()[0].getXCoordinate()