-8

我对这个完全不知所措。这是到目前为止的说明和代码:

import java.util.*;

abstract public class AbstractGamePiece
{

    // These two constants define the Outlaws and Posse teams
    static public final int PLAYER_OUTLAWS = 0;
    static public final int PLAYER_POSSE = 1;

    // These variables hold the piece's column and row index
    protected int myCol;
    protected int myRow;

    // This variable indicates which team the piece belongs to
    protected int myPlayerType;

    // These two strings contain the piece's full name and first letter abbreviation
    private String myAbbreviation;
    private String myName;

    // All derived classes will need to implement this method
    abstract public boolean hasEscaped();

    // Initialize the member variables with the provided data.
    public AbstractGamePiece(String name, String abbreviation, int playerType)
    {

    }

}

我需要帮助的是完成公共 AbstractGamePiece(...) 部分下的代码。

4

1 回答 1

3

试图让你在不为你写下整个事情的情况下继续前进:

对于第 1 点,目标是根据传递给构造函数的参数初始化内部变量(已在类中定义):

public AbstractGamePiece(String name, String abbreviation, int playerType) {
    myName = name;
    // and so on
}

然后,“getter”类型函数返回当前对象中可用的值,如下所示

public int getPlayerType() {
    return myPlayerType;
}

Setter 是相反的,它们根据传递的参数设置内部变量:

public void setPosition(int col, int row) {
    myRow = row;
    myCol = col;
}

等等。

然后,根据说明,您必须使用这个抽象类作为几个具体类的基础:

public class Henchman extends AbstractGamePiece {

    // the constructor - not sure what exactly should be passed in here
    // but you get the idea - this constructor doesn't have to have the
    // same "signature" as super's
    public Henchman(String name) {
        super(name, "hm", PLAYER_OUTLAWS);
    }

    // an an implementation of abstract method hasEscaped
    @Override
    public boolean hasEscaped() {
        return false;  // as per the instructions
    }

}

toString 方法将当前对象的特定描述作为(人类可读的)字符串返回,并且它可以例如用于打印当前片段的人类可读列表,以便在您开始开发游戏引擎后帮助分析/调试游戏. 正如说明所说,它的作用取决于你,让它返回所有有趣和识别信息。为了让您开始,对于 Henchman:

public toString() {
    String.format("Henchman name=%s team=%d escaped=%",myName,myTeam,hasEscaped());
}

但是有 1000 种同样适用的变体。

这应该可以帮助您入门,如果您以后遇到困难,请不要犹豫创建一个新问题。祝你好运!

于 2013-01-11T18:06:57.647 回答