试图通过setter和getter增加一个值,但是值不会增加?

时间:2016-10-03 21:47:23

标签: java

在我的帮助下,我应该修改一个greenfoot项目,通过创建私有数据类型并使用setter和getter来展示对数据封装的理解,以及在游戏中触摸和遗漏的病毒和细菌的值,以及修改评分系统。我能够让得分系统起作用,但是当我运行游戏时,它只会将setter的值递增1并且不会计算通过它。

 public class Bloodstream extends World
{
    private int score;
    private int time;

    /**New Private variables **/

    private int numberOfBacteriaTouched;
    private int numberOfBacteriaMissed;
    private int numberOfVirusTouched;
    private int numberOfVirusMissed;

    /**
     * Constructor: Set up the staring objects.
     */
    public Bloodstream()
    {    
        super(780, 360, 1); 
        setPaintOrder(Border.class);
        prepare();
        score = 0;
        time = 2000;
        this.numberOfBacteriaTouched= 0;
        this.numberOfBacteriaMissed = numberOfBacteriaMissed;
        this.numberOfVirusTouched = 0;
        this.numberOfVirusMissed = numberOfVirusMissed;
        showScore();
        showTime();
    }

    /**
     * Create new floating objects at irregular intervals.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Bacteria(), 779, Greenfoot.getRandomNumber(360));
        }

        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 0);
        }

        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 359);
        }

        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Virus(), 779, Greenfoot.getRandomNumber(360));
        }

        if (Greenfoot.getRandomNumber(100) < 6)
        {
            addObject(new RedCell(), 779, Greenfoot.getRandomNumber(360));
        }
        countTime();
    }

    /** Getters for new variables **/

    public int getNumberOfBacteriaTouched()
    {
        return numberOfBacteriaTouched;
    }

    public int getNumberBacteriaMissed()
    {
        return numberOfBacteriaMissed;
    }

    public int getNumberOfVirusTouched()
    {
        return numberOfVirusTouched;
    }

    public int getNumberOfVirusMissed()
    {
        return numberOfVirusMissed;
    }

        /** Setters for new variables **/

    public void setNumberOfBacteriaTouched(int numberOfBacteriaTouched)
    {
        this.numberOfBacteriaTouched = numberOfBacteriaTouched;
    }

    public void setNumberOfBacteriaMissed(int numberOfBacteriaMissed)
    {
        this.numberOfBacteriaMissed= numberOfBacteriaMissed;
    }

    public void setNumberOfVirusTouched(int numberOfVirusTouched)
    {
        this.numberOfVirusTouched= numberOfVirusTouched;
    }

    public void setNumberOfVirusMissed(int numberOfVirusMissed)
    {
        this.numberOfVirusMissed= numberOfVirusMissed;
    }


    /**
     * Add some points to our current score. (May be negative.)
     * If the score falls below 0, game's up.
     */
    public void addScore(int points)
    {
        score = score + points;
        showScore();
        if (score < 0) 
        {
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
        }
    }

    /**
     * Show our current score on screen.
     */
    private void showScore()
    {
        showText("Score: " + score, 80, 25);
        showText("Bacteria Eaten: " + getNumberOfBacteriaTouched(), 80, 325);
        showText("Virus Missed: " + getNumberOfVirusMissed(), 80, 300);
    }

    /**
     * Count down the game time and display it. Stop the game 
     * with a winning message when time is up.
     */
    private void countTime()
    {
        time--;
        showTime();
        if (time == 0)
        {
            showEndMessage();
            Greenfoot.stop();
        }
    }

    /**
     * Show the remaining game time on screen.
     */
    private void showTime()
    {
        showText("Time: " + time, 700, 25);
    }

    /**
     * Show the end-of-game message on screen.
     */
    private void showEndMessage()
    {
        showText("Time is up - you win!", 390, 150);
        showText("Your final score: " + score + " points", 390, 170);
    }

    /**
     * Prepare the world for the start of the program. In this case: Create
     * a white blood cell and the lining at the edge of the blood stream.
     */
    private void prepare()
    {
        WhiteCell whitecell = new WhiteCell();
        addObject(whitecell, 128, 179);

        Lining lining = new Lining();
        addObject(lining, 126, 1);
        Lining lining2 = new Lining();
        addObject(lining2, 342, 5);
        Lining lining3 = new Lining();
        addObject(lining3, 589, 2);
        Lining lining4 = new Lining();
        addObject(lining4, 695, 5);
        Lining lining5 = new Lining();
        addObject(lining5, 114, 359);
        Lining lining6 = new Lining();
        Lining lining7 = new Lining();
        addObject(lining7, 295, 353);
        Lining lining8 = new Lining();
        Lining lining9 = new Lining();
        Lining lining10 = new Lining();
        addObject(lining10, 480, 358);
        Lining lining11 = new Lining();
        addObject(lining11, 596, 359);
        Lining lining12 = new Lining();
        addObject(lining12, 740, 354);

        Border border = new Border();
        addObject(border, 0, 180);
        Border border2 = new Border();
        addObject(border2, 770, 180);
    }
}

这是主类的副本,这里是计数器的子类吗?

    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class WhiteCell extends Actor
{
    /**
     * Act: move up and down when cursor keys are pressed.
     */
    public void act() 
    {
        checkKeyPress();
        checkCollision();
    }

    /**
     * Check whether a keyboard key has been pressed and react if it has.
     */
    private void checkKeyPress()
    {
        if (Greenfoot.isKeyDown("up")) 
        {
            setLocation(getX(), getY()-8);
        }

        if (Greenfoot.isKeyDown("down")) 
        {
            setLocation(getX(), getY()+8);
        }

        if (Greenfoot.isKeyDown("right")) 
        {
            setLocation(getX()+4, getY());
        }

        if (Greenfoot.isKeyDown("left")) 
        {
            setLocation(getX()-4, getY());
        }
    }

    /**
     * Check whether we are touching a bacterium or virus. Remove bacteria.
     * Game over if we hit a virus.
     */
    private void checkCollision()
    {
        if (isTouching(Bacteria.class)) 
        {
            Greenfoot.playSound("slurp.wav");
            removeTouching(Bacteria.class);
            Bloodstream bloodstream = (Bloodstream)getWorld();
            bloodstream.addScore(1);
            bloodstream.setNumberOfBacteriaTouched(+1);
        }

        if (isTouching(Virus.class)) 
        {
            removeTouching(Virus.class);
            Bloodstream bloodstream = (Bloodstream)getWorld();
            bloodstream.addScore(-2);
            bloodstream.setNumberOfVirusTouched(+1);
        }
    }
}

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

如果查看setter代码,则意味着设置值,而不是增量。每次执行时,它都会将numberOfBacteriaTouched的值设置为1。这就是为什么你的计数器第一次工作,但不计算第一次。

    public void setNumberOfBacteriaTouched(int numberOfBacteriaTouched)
    {
        this.numberOfBacteriaTouched = numberOfBacteriaTouched;
    }

增加值的正确方法是使用getter获取值,将值加1并使用setter设置它。

    bloodstream.setNumberOfBacteriaTouched(bloodstream.getNumberOfBacteriaTouched()+1);
    bloodstream.setNumberOfVirusTouched(bloodstream.getNumberOfVirusTouched()+1);

或者,您也可以编写一个不同的方法,将变量递增1.

    public void incrementNumberOfBacteriaTouched()
    {
        this.numberOfBacteriaTouched++;
    }

答案 1 :(得分:0)

public void setNumberOfBacteriaTouched(int numberOfBacteriaTouched)
{
    this.numberOfBacteriaTouched = numberOfBacteriaTouched;
}

看一下该方法正在做什么。它将触摸的数字设置为您传入的任何值。您没有添加该值,而是设置一个全新的值。

答案 2 :(得分:0)

每次调用checkCollision()时,您都会重新初始化Bloodstream类。尝试将实例化移出checkCollision()方法

    private Bloodstream bloodstream;
      onCreate(){
      bloodstream = (Bloodstream)getWorld();          
   } 
   private void checkCollision(){
        if (isTouching(Bacteria.class)) 
        {
            Greenfoot.playSound("slurp.wav");
            removeTouching(Bacteria.class);
            bloodstream.addScore(1);
            bloodstream.setNumberOfBacteriaTouched(+1);
        }

        if (isTouching(Virus.class)) 
        {
            removeTouching(Virus.class);
            bloodstream.addScore(-2);
            bloodstream.setNumberOfVirusTouched(+1);
        }
    }