在一个类中构建对象并在另一个类中访问它们

时间:2013-04-22 03:18:17

标签: java object

我正在创建一个选择你自己的冒险“游戏”作为团队项目的一部分。我的想法是创建一个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);
    }

}

3 个答案:

答案 0 :(得分:3)

在您发布的代码中,entryway不是实例变量,它是方法变量 - 其范围仅限于您的AdventureBuilder.main()方法。如果您希望它可以在类之外和该方法之外访问,请将其设置为实例变量(或返回它,然后调用方法可以使用它)。

答案 1 :(得分: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的副本。

答案 2 :(得分:1)

根据凯瑟琳的建议修改代码,这应该是它的样子。

...

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);
    }
}