2017-03-28 7 views
-2

私はゲーム「The Oregon Trail」の再作成に取り組んでおり、オブジェクト配列を作成しましたが、スーパークラス内のメソッドの使い方を理解することはできません。私はスーパークラスLocationとサブクラスCityFortRiverLandmarkを持っています。ここでは、配列をインスタンス化するための私のコードは次のとおりです。配列に入れたオブジェクト内のメソッドにアクセスするにはどうすればいいですか?

City  independence  = new City("Independence", 102); 
    River kansas   = new River("Kansas", 82); 
    River bigBlue   = new River("Big Blue River", 118); 
    Fort  kearney   = new Fort("Fort Kearney", 86); 
    Landmark chimneyRock  = new Landmark("Chimney Rock", 86); 
    Fort  laramie   = new Fort("Fort Laramie", 190); 
    Landmark independenceRock = new Landmark("Independence Rock", 102); 
    Landmark southPass   = new Landmark("South Pass", 57, 125); 
    River green    = new River("Green River", 143); 
    Fort  bridger   = new Fort("Fort Bridger", 162); 
    Landmark sodaSprings  = new Landmark("Soda Springs", 57); 
    Fort  hall    = new Fort("Fort Hall", 182); 
    River snake    = new River("Snake River", 113); 
    Fort  boise    = new Fort("Fort Boise", 160); 
    Landmark blueMountains  = new Landmark("Blue Mountains", 55, 125); 
    Fort  wallaWalla  = new Fort("Fort Walla Walla", 120); 
    Landmark dalles   = new Landmark("The Dalles", 100); 

    kansas.setWidth(620); 
    kansas.setDepth(4); 
    bigBlue.setWidth(300); 
    bigBlue.setDepth(6); 
    green.setWidth(400); 
    green.setDepth(20); 
    snake.setWidth(1000); 
    snake.setDepth(7); 

    Object[] locations = new Object[] { 
     independence, 
     kansas, 
     bigBlue, 
     kearney, 
     chimneyRock, 
     laramie, 
     independenceRock, 
     southPass, 
     green, 
     bridger, 
     sodaSprings, 
     hall, 
     snake, 
     boise, 
     blueMountains, 
     wallaWalla, 
     dalles 
    }; 

クラスのインスタンス化のためのパラメータはどちらか(文字列名、次のランドマークにint型の距離)または(文字列名、オプションAにint型の距離、オプションBまでの距離)です道路にフォークがあるので。しかし、それは私の質問には関係ありません。

+2

これは、 'Location [] locations = new Location [] {'(または、配列の代わりに 'List 'を使うのが良い)と宣言します。 – 4castle

+0

どうすればリストを使用できますか?私は将来どのようにそれらを呼び戻すでしょうか?私はもともと 'Location [] locations = new location [] {'としていたので、うまく動作しませんでした。 –

+0

'List'の使い方を示すことは、おそらくチュートリアルにとっては良い仕事です。あなたはそれが動作していないときにそれを使用しようとした方法を表示していただけますか? – 4castle

答えて

0

どのような方法で呼び出したいですか?

あなたが同じ配列または同じコレクション内のオブジェクトの束を入れしようとしている場合は、上のすべてのそれらの(または、可能性を実行する予定で、1つのまたは複数の共通の方法がありますので、通常、それはだいかなるそれらの上で行う。)

その場合、あなたは通常、それはオブジェクトすべてに共通していることは何でもキャプチャinterfaceを宣言したいと思います。

この例では、Landmark,FortおよびRiverのオブジェクトがあります。彼らの共通点は何がありますか?まあ、あるもののために、彼らはすべて名前を持っています。だから、

interface Location { 
    String getName(); 
    ...other methods that apply to all different kinds of location... 
} 

を持っているかもしれません。そして、あなたはこのようなあなたの配列を宣言した場合...

Location locations[] = new Location[] { independence, kansas, bigBlue, ...}; 

コンパイラはあなたがいずれかを適用することができます、そして、

class Landmark implements Location { 
    String getName() { return ...; } 
    ...other methods that apply to all different kinds of location... 
    ...other methods that only apply to Landmarks... 
} 

class Fort implements Location { 
    String getName() { return ...; } 
    ...other methods that apply to all different kinds of location... 
    ...other methods that only apply to Forts... 
} 

etc. 

を持っているかもしれませんLocationのメソッドを配列のメンバに渡します。

for (int i=0 ; i<locations.length ; i++) { 
    System.out.println(locations[i].getName()); 
} 
関連する問題