[英]Calling methods from other classes java
我剛剛開始用Greenfoot編程一些東西,並且一直學習Java。 我對如何在類之間調用某些方法以及靜態和非靜態之間的區別已經很熟悉。
我正在做一個游戲,您在其中玩螃蟹並四處走動以收集蠕蟲。 有一只龍蝦隨機游盪,如果與螃蟹接觸,螃蟹就會消失。 每次吃蠕蟲,分數都會提高10。
它包含5個名為Crab,Lobster,Worm,Counter和CrabWorld的類。 為了您的方便,我將發布記錄良好的代碼供您閱讀。 但是,我遇到的重要問題是調用從龍蝦到CrabWorld創建的Crab實例的方法。 這種方法會改變螃蟹的生活。
我已經嘗試調用((CrabWorld)getWorld),但是不需要訪問CrabWorld。 我需要從Lobster類訪問Crab實例(在CrabWorld中創建,如果有關系的話)。
螃蟹世界:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Creates the crab Enviornment with a counter, crab and lobster. Also has methods to
* place random worms in the world over time. Also has a method to add the score
* To the Counter score
* @author Troy Bick
* @version 12/20/13
*/
public class CrabWorld extends World
{
private Actor playerCrab = new Crab();
private Counter score = new Counter("Score: ");
/**
* Constructor for objects of class CrabWorld.
*
*/
public CrabWorld()
{
super(560, 560, 1);
prepare();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
addObject(score, 100, 540);
addObject(playerCrab, 280, 280);
addObject(new Lobster(), 100, 100);
}
/**
* Randomly places worms at random periods
*/
public void act()
{
if(Greenfoot.getRandomNumber(100)<0.5){
addObject(new Worm(), Greenfoot.getRandomNumber(540)+10, Greenfoot.getRandomNumber(540)+10);
}
}
public void eatenWorm()
{
score.add(10);
}
public void eatsCrab()
{
playerCrab.isEaten();
}
螃蟹:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Crab extends Actor
{
private int wormsEaten = 0;
private int lives = 3;
/**
* Act - do whatever the Crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment. Run calls Act every
* frame.
*/
public void act()
{
moveAndTurn();
eat();
}
/**
* Determines the key pressed and moves/turns the crab.
*/
public void moveAndTurn()
{
move(3);
if(Greenfoot.isKeyDown("a")){
turn(3);
}
if(Greenfoot.isKeyDown("d")){
turn(-3);
}
}
/**
* Detects if the worm is close to the crab, and if so, eats it.
*/
public void eat()
{
Actor worm;
worm = getOneObjectAtOffset(0,0,Worm.class);
World world = getWorld();
if(worm != null)
{
world.removeObject(worm);
Greenfoot.playSound("eating.wav");
wormsEaten++;
((CrabWorld) getWorld()).eatenWorm();
}
}
/**
* Returns the number of worms eaten.
*/
public int getWormsEaten()
{
return wormsEaten;
}
/**
* Returns the number the lives the crab has left.
*/
public int getLivesCount()
{
return lives;
}
/**
* Subtracts a life from lives.
*/
public void eaten()
{
lives--;
}
}
龍蝦:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Lobster here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lobster extends Actor
{
/**
* Act - do whatever the Lobster wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moveAround();
eat();
}
public void eat()
{
Actor crab;
crab = getOneObjectAtOffset(0,0,Crab.class);
if(crab != null)
{
World world = getWorld();
world.removeObject(crab);
((CrabWorld) getWorld()).eatsCrab();
}
}
public void moveAround()
{
move(3);
//Random Movements
if(Greenfoot.getRandomNumber(100) < 10)
{
turn(Greenfoot.getRandomNumber(40) - 20);
}
//World Edge Detection
if(getX() <= 10 || getX() >= getWorld().getWidth()-10)
{
turn(10);
}
if(getY() <= 10 || getY() >= getWorld().getHeight()-10)
{
turn(10);
}
}
}
因此,當添加到世界上的龍蝦吃掉螃蟹時,我希望那只螃蟹喪命,但是當我嘗試編譯時,在CrabWorld類上遇到了一個錯誤,即找不到所提到的方法。 這是為什么?
只是非常困惑...如果有人可以幫助我,那將是很棒的。 如果我丟失任何東西,我會解決。
謝謝,特洛伊
因此,看來您在龍蝦類中有一個getWorld方法,我假設它能獲得整個世界。 如果在CrabWorld中添加getCrab方法,則龍蝦可以訪問螃蟹。
CrabWorld內部
public Crab getCrab(){
return playerCrab;
}
然后在龍蝦
((CrabWorld) world).getCrab();
你的問題是,在CrabWorld
聲明playerCrab
為
private Actor playerCrab = new Crab();
因此,盡管playerCrab實際上是Crab的實例,但對於CrabWorld來說,它是一個Actor,因此您只能調用Actor定義的方法。
您可以將聲明更改為
private Crab playerCrab = new Crab();
這樣CrabWorld知道playerCrab
是Crab
的實例,然后您可以調用Crab
定義的任何公共方法。 假設您的成員變量稱為playerCrab
,似乎您將始終將其作為Crab
,因此將其聲明為一個當然是適當的。
如果您認為失去生命的概念是擴展Actor
類所loseLife()
,那么,如果您控制Actor
類型,則可以采用另一種方法,即為其添加一個loseLife()
方法。 您的Lobster
類目前沒有這個概念,但是如果您決定將其添加到該類中,那么Actor
上的方法將是實現此概念的合適方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.