如何使用某个类中另一个类的方法?

时间:2013-03-24 03:46:29

标签: java oop class object

我这里有两个课程,在我的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                    
}

1 个答案:

答案 0 :(得分:1)

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