-2

子类:下面是一个子类,我从中将变量分类为父类。

public class Score extends Actor
{
    static int sum = 0;  //NOT this SUM

    public void act()
    {
    }

    public int getScore(int scoreDisplay)
    {
        int mX, mY;
        Message mess = new Message();

        mX = 720;
        mY = 50;
        World wld = getWorld();
        wld.addObject(mess, mX, mY);
        sum = sum + scoreDisplay;  // I want this sum.
        mess.displayMessage(sum);
        return sum;
    }
}

我想调用sum=sum+scoreDisplay;父类中的值。

父类:

public class BalloonWorld extends World
{
    private Score score;
    Timer timerText = new Timer();
    private int timer = 3600;

    public Score getScore()
    {
        return score;
    }

    public BalloonWorld()
    {
        super(800, 600, 1);
        addObject(timerText, 100, 15);
        timerText.setText("Time left: " + (timer / 60));
        prepare();
    }

    private void prepare()
    {
        Gun g1 = new Gun();
        addObject(g1, 0, 300);
        addObject(new Timer(), 780, 300);

        score = new Score();
        addObject(score, 533, 291);

        if (score.sum > 100) // I want the sum to be called here, if its value is greater than 100      the 'if' statement should execute.
        {
            Gun2 g2 = new Gun2();
            addObject(g1, 0, 300);
            addObject(new Timer(), 780, 300);
        }

        if (timer > 0)
        {
            timer--;
            if (timer % 60 == 0)
            {
                timerText.setText("Time left: " + (timer / 60));
            }
        }
        if (timer == 0)
        {
            Greenfoot.stop();
        }
    }
}

如何将子类中的变量调用到父类中。

4

1 回答 1

0

你要这个:

Score score = new Score();
score.getScore();
于 2013-11-11T08:10:31.177 回答