在主类中调用变量

时间:2013-11-11 07:34:54

标签: java

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

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();
        }
    }
}

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

1 个答案:

答案 0 :(得分:0)

你想要这个:

Score score = new Score();
score.getScore();