2013-04-22 88 views
1

我正在創建一個選擇您自己的冒險「遊戲」作爲組項目的一部分。我的想法是創建一個StoryRoom類型的對象,它可以存儲諸如StoryRoom的描述,其他StoryRooms連接到它的信息等。我還想創建一個冒險類,這將成爲一個StoryRooms數組。這個想法是,主程序將在StoryRooms中循環,顯示文本並在作出決定時返回一個值,該值將成爲下一個StoryRoom的參考,直到玩家達到最終場景。在一個類中構建對象並在另一個類中訪問它們

我遇到的問題是我無法通過AdventureTeller類訪問在AdventureBuilder類中創建的StoryRooms。 AdventureTeller是遊戲的驅動程序類。 Eclipse會拋出一個入口無法作爲變量解析的錯誤。我怎樣才能解決這個問題?

public class StoryRoom { 
    public String[] description = null; 
    public int[] exits; 


    public StoryRoom(String[] builddescriptions, int[] buildexits){ 
     description = new String[builddescriptions.length]; 
     exits = new int[buildexits.length]; 
     for (int i = 0; i< builddescriptions.length; i++){ 
      description[i] = builddescriptions[i]; 
     } 
     for (int i = 0; i< buildexits.length; i++){ 
      exits[i] = buildexits[i]; 
     } 

    } 

    public String GetDescription(int reference){ 
     return description[reference]; 
    } 

    public int GetExit(int reference){ 
     return exits[reference]; 
    } 

} 


public class AdventureTeller { 


    public static void main(String[] args) { 
     AdventureBuilder.main(); 
     for (int i = 0; i < entryway.description.length; i++) 
     { 
      System.out.println(entryway.GetDescription(i)); 
     } 

    } 

} 




public class AdventureBuilder { 

    public static void main() { 
     // StoryRoom[] book = new StoryRoom[20]; 
     String[] description = null; 
     description = new String[3]; 
     int[] exits; 
     exits = new int[3]; 

     description[0] = "This is a strange room with 2 doors, one on the left and one on the right."; 
     description[1] = "Take the left door? (enter 1)"; 
     description[2] = "Take the right door? (enter 2)"; 
     exits[0] = 1; // current room number 
     exits[1] = 2; 
     exits[2] = 3; 
     StoryRoom entryway = new StoryRoom(description, exits); 
    } 

} 
+0

我認爲它與對象的範圍有關,並且只要AdventureBuilder的main運行完畢,它們就會被銷燬。我嘗試將對象或數組對象(註釋代碼)作爲返回值傳遞回去,但是並沒有成功。 – 2013-04-22 03:23:46

回答

2

單獨更改AdventureBuilder將不夠用。您還需要更改AdventureTeller。下面兩個類的新版本(測試工作在Java 1.6):

public class AdventureBuilder 
{ 
    private StoryRoom entryway; 

    // Avoid naming a method 'main' unless it's the main method of your main class 
    // Try something like 'initAdventureBuilder()' instead 
    public void main() 
    { 
     // StoryRoom[] book = new StoryRoom[20]; 
     String[] description = null; 
     description = new String[3]; 
     int[] exits; 
     exits = new int[3]; 

     description[0] = "This is a strange room with 2 doors, one on the left and one on the right."; 
     description[1] = "Take the left door? (enter 1)"; 
     description[2] = "Take the right door? (enter 2)"; 

     exits[0] = 1; // current room number 
     exits[1] = 2; 
     exits[2] = 3; 

     entryway = new StoryRoom(description, exits); 
    } 

    // A way for AdventureBuilder to be questioned about its entryway field 

    public StoryRoom getStoryRoom() 
    { 
     return entryway; 
    } 
} 

public class AdventureTeller 
{ 
    public static void main(String[] args) 
    { 
     // Creating a copy of AdventureBuilder that AdventureTeller knows about 
     AdventureBuilder ab = new AdventureBuilder(); 
     StoryRoom entryway; 

     // Asking AdventureBuilder to run itself 
     ab.main(); 

     // Asking AdventureBuilder for a reference to the entryway which it created 
     // when you asked AdventureBuilder to run itself 

     entryway = ab.getStoryRoom(); 

     for (int i = 0; i < entryway.description.length; i++) 
     { 
      System.out.println(entryway.GetDescription(i)); 
     } 
    } 
} 

你可能想避免使用main作爲比啓動程序之外的其他任何一種方法的名稱。嘗試使用像initAdventureBuilder()之類的描述性方法名稱,以避免混淆。當我第一次看到你的代碼時,我認爲AdventureBuilder是主要的類,因爲你的命名約定,事實上,它不是。

爲了讓您知道發生了什麼,當您嘗試編譯時,編譯器抱怨說不知道AdventureTeller中的入口字段是指什麼。即使你只是爲了編譯而註釋掉了這行代碼,AdventureTeller仍然不知道AdventureBuilder中的字段是什麼,除非你明確告訴AdventureTeller那裏有什麼。更糟糕的是,你從來沒有在AdventureTeller中創建AdventureBuilder的副本。

3

在你發佈的代碼,entryway不是一個實例變量,這是一個方法變量 - 其範圍僅限於您AdventureBuilder.main()方法。如果你希望它可以在類之外和那個方法之外訪問,那麼把它作爲一個實例變量(或返回它,然後調用方法可以使用它)。

1

只需根據Catherine的建議修復代碼,這應該看起來像。

... 

public class AdventureBuilder { 

    StoryRoom entryway; 

    public static void main() { 
     // StoryRoom[] book = new StoryRoom[20]; 
     String[] description = null; 
     description = new String[3]; 
     int[] exits; 
     exits = new int[3]; 

     description[0] = "This is a strange room with 2 doors, one on the left and one on the right."; 
     description[1] = "Take the left door? (enter 1)"; 
     description[2] = "Take the right door? (enter 2)"; 
     exits[0] = 1; // current room number 
     exits[1] = 2; 
     exits[2] = 3; 
     entryway = new StoryRoom(description, exits); 
    } 
} 
相關問題