Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在另一个对象中添加对象?_Java_Class_Oop_Object - Fatal编程技术网

Java 如何在另一个对象中添加对象?

Java 如何在另一个对象中添加对象?,java,class,oop,object,Java,Class,Oop,Object,我正在尝试创建一个控制台象棋游戏。 我有正方形类,我的棋盘有64个正方形对象。此外,这些正方形中的一些还具有块对象如何将这些工件对象添加到方形对象中?* public class Square { int column; int row; Piece piece; Square(){ } public void setRow(int row) { this.row=row; } public void s

我正在尝试创建一个控制台象棋游戏。 我有正方形类,我的棋盘有64个正方形对象。此外,这些正方形中的一些还具有块对象如何将这些工件对象添加到方形对象中?*

public class Square {

    int column;
    int row;
    Piece piece;

    Square(){

    }



    public void setRow(int row) {
        this.row=row;
    }
    public void setColumn(int col){
        this.column=col;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        if(1< row && row <6)
            return " ";
        return super.toString();
    }
}
公共类广场{
int列;
int行;
一件一件;
正方形(){
}
公共无效集合行(整数行){
this.row=行;
}
公共无效设置列(整数列){
this.column=col;
}
@凌驾
公共字符串toString(){
//TODO自动生成的方法存根
如果(1)小于行和行

将其添加到类中可以为每个正方形设置一个片段。您还可以在正方形的构造函数中定义片段,因为这样可以确保正方形始终有一个已定义的片段。

您可以将这些有用的方法添加到
square
类中,以便在所需的所有正方形之间设置各种片段

/**
 * @param piece to set on the actual square.
 */
public void setPiece(Piece piece) {
    this.piece = piece;
}

/**
 * Removes the actual piece on the square, if it exists.
 */
public void removePiece() {
    if(piece != null)
        piece = null;
}

/**
 * @param square where to move the piece on the actual square.
 * @return true if the piece were moved correctly, false otherwise.
 * e.g. of a false return is when the piece on the actual square is not present, in other words, it is null.
 * 
 * ATTENTION: Obviously the moved piece is removed from the actual square.
 */
public boolean movePiece(Square square) {
    if(piece != null) {
        square.setPiece(piece);
        removePiece();
        return true;
    } else
        return false;
}

通过设置块方法?我不想为每个方块添加块。我只想将它们添加到32个方块。而且每个块都可以移动,这样它们就可以更改方块。我真的很困惑“我只想将它们添加到32个方块。”你能详细解释一下吗?@SachinKumar,我的棋盘需要32块。我的棋盘有64个方块。我的意思是,我有一个棋盘有64个方块,我有64个方块,其中一些方块有碎片。我总共有32块。
/**
 * @param piece to set on the actual square.
 */
public void setPiece(Piece piece) {
    this.piece = piece;
}

/**
 * Removes the actual piece on the square, if it exists.
 */
public void removePiece() {
    if(piece != null)
        piece = null;
}

/**
 * @param square where to move the piece on the actual square.
 * @return true if the piece were moved correctly, false otherwise.
 * e.g. of a false return is when the piece on the actual square is not present, in other words, it is null.
 * 
 * ATTENTION: Obviously the moved piece is removed from the actual square.
 */
public boolean movePiece(Square square) {
    if(piece != null) {
        square.setPiece(piece);
        removePiece();
        return true;
    } else
        return false;
}