2012-12-04 36 views
0

我正在做一個項目,我需要一些幫助。我有一個AnimalWorld延伸WorldElephant類延伸Turtle。在我的Elephant的act()方法中,我想要使用僅在World(和AnimalWorld)中找到的方法(public List getTurtleList())。我試過addiding行如何在不同的課程中使用方法?

public abstract List<Turtle> getTurtleList(); 

到AnimalWorld,並設置大象一個抽象類,但我仍然得到錯誤「無法找到符號 - getTurtleList() 有什麼想法?如果您需要更多信息,請告訴我。此外,大象不會擴展動物世界,因爲大象創造的動物世界將被放置。

public abstract class Elephant extends Turtle implements Animal{ 

    public Elephant(ModelDisplay world){ 

     super(world); 
     this.penUp(); 
     this.setColor(Color.gray); 
     this.setWidth(50); 
     this.setHeight(50); 
    } 

    public void act(ModelDisplay myWorld){ 
     double smallestDist = 640.0; 

     Mouse closestMouse = null; 
     List<Turtle> animalList = myWorld.getTurtleList(); 

     for(Turtle curCritter: animalList){ 
      if(curCritter instanceof Mouse){ 
       int x=curCritter.getXPos(); 
       int y=curCritter.getYPos(); 
       double dist = this.getDistance(x,y); 

       if(dist < 100 && dist<smallestDist){ 
        closestMouse=curCritter; 
        smallestDist=dist; 
       } 
      } 
     } 

     if(closestMouse!=null){ 
      this.turn(closestMouse); 
      this.forward(3*(int)smallestDist); 
     } 
    } 
} 
+20

「擴展烏龜的大象類」不是很奇怪 – Abubakkar

+3

我建議你發佈整個代碼和你的錯誤代碼。它是功課嗎? –

+5

@阿布有時大象可以是海龜,在極少數情況下,它們甚至可以飛翔。 – Maroun

回答

0

聲明World類爲:

public abstract class World{ 
    ... 

    public abstract List<Turtle> getTurtleList(); 
} 

將迫使你實現一個方法,在每一個從World繼承類稱爲getTurtleList()。如果不是的話,你將被迫聲明這個子類也是抽象的。這就是爲什麼你不得不聲明Elephant爲抽象類,因爲它沒有實現超類的所有抽象方法。

也許你需要執行getTurtleList()AnimalWorldElephant

+0

你是什麼意思,在'象'執行'?大象已經實施動物。我也不能將世界宣佈爲抽象。這是行不通的。 –

+0

對不起,我不明白你的班級圖。你可以發佈嗎? – jmrodrigg

相關問題