0

因此,对于我的课程,我必须使用 Greenfoot IDE。我的目标是:

“猪吃掉了桶里所有的蘑菇”</p>

猪将不得不调用桶的getMushroom()方法来找出桶当前存储了多少蘑菇。猪可以将此数量添加到其食用蘑菇的数量中。请记住,蘑菇已经从世界上消失了——这发生在桶“储存它们”时。</p>

但是,如果我尝试进入 Pig 类并使用它,则表示无法从静态上下文中引用Barrel.getMushrooms();非静态方法。getMushrooms()

但是当我尝试使用类似的东西时

Barrel b1 = new Barrel();
b1.getMushrooms();

我的带蘑菇的柜台从来没有成功过..

类桶

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

 /**
  * Write a description of class Barrel here.
  * 
  * @author (your name) 
  * @version (a version number or a date)
  */
 public class Barrel extends Actor
 {
     private final double SCALE_FACTOR_5 = 1.05;
     private final double SCALE_FACTOR_25 = 1.25;
     public int mushroomsStored;
     private int ns;

     public Barrel() {
         mushroomsStored = 0;
         ns = 0;
     }

     /**
      * Main method of Barrel
      */
     public void act() {
         followMouse();
         storeMushrooms();
         reset();
     }  

     /**
      * Follows mouse drag-and-drop motion.
      */
     public void followMouse() {
         if(Greenfoot.mouseDragged(this)) {
             MouseInfo mouse = Greenfoot.getMouseInfo();
             setLocation(mouse.getX(), mouse.getY());
         }
     }

     /**
      * Eats nearby mushrooms when dropped.
      * Increases its current image scale by 5% when it eats one mushroom.
      * Increases its current image scale by 25% when it eats five mushrooms.
      * If this barrel stores more than 10 mushrooms, this barrel has itself removed from
      * this world.
      */
     public void storeMushrooms() {
         if(Greenfoot.mouseDragEnded(this)) {
             List<Mushroom> nearby = getObjectsInRange(75, Mushroom.class);
             ns = nearby.size();
             for(Mushroom m : nearby) {
                 getWorld().removeObject(m);
                 mushroomsStored++;
             }
             if(ns < 5 ) {
                 GreenfootImage img = getImage();
                 int width = (int)(img.getWidth() * SCALE_FACTOR_5);
                 int height = (int)(img.getHeight() * SCALE_FACTOR_5);
                 img.scale(width, height);
             }
             if (ns >= 5) {
                 GreenfootImage img = getImage();
                 int width = (int)(img.getWidth() * SCALE_FACTOR_25);
                 int height = (int)(img.getHeight() * SCALE_FACTOR_25);
                 img.scale(width, height);
             }
             if(mushroomsStored == 10) {
                 getWorld().removeObject(this);
             }
         }
     }

     /**
      * Returns this barrel to its original (x,y) location and its
      * original image scale.
      */
     public void reset() {
         if(Greenfoot.mouseClicked(this)) {
             MouseInfo mouse = Greenfoot.getMouseInfo();
             if(mouse.getButton() == 3) {
                 this.setLocation(565, 350);
                 setImage(new GreenfootImage("barrel.png"));
                 mushroomsStored = 0;
             }
         }
     }

     /**
      * Returns how many mushrooms this barrel has stored.
      */
     public int getMushrooms() {
         return mushroomsStored;
     }

     /**
      * Automatically called by Greenfoot whenever a Barrel object
      * is placed in a world. In this assigment, we use it to remember
      * the initial state of this barrel - its (x,y) position and its
      * original image size.
      */
     public void addedToWorld(World world) {
         GreenfootImage img = new GreenfootImage("barrel.png");
         setImage(img);
         final int originalX = getX();
         final int originalY = getY();
         final int originalWidth = img.getWidth();
         final int originalHeight = img.getHeight();
     }
 }

猪类

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

 /**
  * Write a description of class Pig here.
  * 
  * @author (your name) 
  * @version (a version number or a date)
  */
 public class Pig extends Actor
 {
     /** Keeps track of how many mushrooms this pig has eaten. */
     private int shrooms;

     /** 
      * Constructs a Pig object and initializes it as having
      * eaten no mushrooms. 
      */
     public Pig() {
         shrooms = 0;
     }

     /**
      * Follows the mouse movement and eats mushrooms on mouse clicks.
      * Stops the scenario once this pig has eaten at least 15 mushrooms.
      */
     public void act() 
     {
         followMouse();
         eatMushrooms();
         getMS();
     }

     public void getMS() {
         b
     }

     public void followMouse() {
         if (Greenfoot.mouseMoved(null)) {
             MouseInfo mouse = Greenfoot.getMouseInfo();
             setLocation(mouse.getX(), mouse.getY());
         }
     }

     public void eatMushrooms() {
         if (Greenfoot.mouseClicked(null)) {
             Mushroom m = (Mushroom) getOneIntersectingObject(Mushroom.class);
             if (m != null) {
                 shrooms++;
                 getWorld().removeObject(m);
             }
         }
         if (shrooms > 29) {
             Greenfoot.stop();
         }
     }
 }
4

2 回答 2

0

当使用类名调用方法时

Barril.getMushrooms()

您的方法应该耦合到您的类而不是实例。你会怎么做?通过使它成为一种static方法。

我假设您希望在此上下文中使用一个实例方法,因此正确的方法是您提出的方法:

Barril b = new Barril();
b.getMushrooms();

但是想想看。mushroomsStored如果你刚刚创建了你的对象,那么它的价值是什么?

... 你猜到了。你甚至没有初始化它,所以它会默认返回值:0

于 2015-10-08T05:36:16.510 回答
0

你的猪需要一个桶类的引用。通常在 Greenfoot 中,A 类通过以下两种方式之一获得对 B 类的引用:

  1. 类 A 在创建时传递对 B 的引用。如果 B 创建 A,这很典型:例如,如果枪发射子弹,枪会传递枪对子弹的引用。

  2. A 类与 B 类发生碰撞,例如您的玩家角色撞到一个收藏品,或者敌人与您的玩家发生碰撞。

我不确定,但我怀疑你属于情况 2,在这种情况下你想使用:

Barrel b = (Barrel)getOneIntersectingObject(Barrel.class);

b当您不接触桶时,该变量将为 null,否则它将是一个 Barrel 对象,您可以在其上调用方法。

于 2015-10-08T20:44:04.107 回答