2013-04-22 100 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); 
    } 
}