1

我这里有两个类,在我的 Player 类中,我想从 Scoresheet 类中获取这些类的总数。我不知道该怎么做。

public class ScoreSheet {

public int getOnes(ArrayList<Integer> die)
{
    for (int i = 0; i < die.size(); i++)
    {
        if (die.get(i) == 1)
        {
            ones++;
        }
    }
    return ones;
}


public class Player {

private int ones = 0;
private int twos = 0;
private int threes = 0;
private int fours = 0;
private int fives = 0;
private int sixes = 0;
private int threeOfKind = 0;
private int fourOfKind = 0;
private int fullHouse = 0;
private int smallStraight = 0;
private int largeStraight = 0;
private int yahtzee = 0;
private int chance = 0;



public void checkScores(ArrayList<Integer> die)
{
    ones = Player -> ScoreSheet.getOnes(<Integer> die); // this is wrong, need to know
                                                        // how to get total                    
}
4

1 回答 1

1

Player 类需要一个 ScoreSheet 变量,该变量初始化为当前的 ScoreSheet 对象。可以通过构造函数参数或setScoreSheet(ScoreSheet scoreSheet)setter 方法将变量设置为正确的对象。然后,玩家可以getOnes(...)在需要时调用 ScoreSheet 变量上的方法或其他 ScoreSheet 方法。

于 2013-03-24T03:49:21.740 回答