1

我的理解是你不能按照我的要求去做。如果您查看下面代码中的星号 ( * ) 注释错误,您可以看到我正在尝试访问的内容。我觉得我需要能够做到这一点,以便我可以使用一种方法来动态创建许多对象,然后从其他对象访问所有这些对象。

有没有办法做到这一点,我错过了,或者我只是把事情搞砸了?如果没有,我应该如何做才能使我获得与以下相同的功能?如果除了传递对象之外还有其他方法可以做到这一点,那将不胜感激(传递对象似乎有很多工作 - 特别是对于多维对象数组 - 应该有一种简单的方法来实例化包私有对象,可以可以在包中的其他任何地方访问)。但是如果传递是唯一的方法,请让我知道最好的方法,特别是当我传递一堆对象的二维数组时。谢谢!

package simpleclasswithinclasstest;

class Game {

    static int boardSize;
    Engine gameEngine;

    Game() {
    }

    public void run() {
        gameEngine = new Engine();
        gameEngine.play();
    }

    public int getBoardSize() {
        return boardSize;
    }
}

class Engine {

    int boardSize;

    Engine() {
    }

    public void play() {

        this.boardSize = currentGame.getBoardSize(); // *****1 Error is here.
        // *****It doesn't recognize currentGame, but I want it to.
    }

    void doNothing() {
    }
}

class Board {

    Board() {
    }

    void Test() {
        gameEngine.doNothing(); // 2 *****Error is here.
        // *****It doesn't recognize gameEngine.
    }
}

public class SimpleClassWithinClassTest {

    static Game currentGame;

    public static void main(String[] args) {
        currentGame = new Game();
        currentGame.run();
    }
}
4

4 回答 4

3

您将gameEngine通过将Board类作为参数传递给Board. 当你实例化你的Board,你可以做这样的事情:

class Engine {
    int boardSize;

    Engine () {
    Board board = new Board(this);
    }

    public void play() {
    }

    void doNothing() {
    // magic stuff in here
    }
}

class Board {
    Engine engine;

    Board (Engine gameEngine) {
    this.engine = gameEngine
    }

    void Test() {
        engine.doNothing(); // No error here :-) and this engine is your main one
    }
}

看一下消息驱动通信的概念。通过阅读此答案,您可能会更清楚。

在我从上面链接的答案中获取的下图中,您可以将其想象f为类中的引擎对象Engine,以及类中c引擎Board。您实际上是在操作同一个对象。

在此处输入图像描述

至于您的其他问题(第一个问题):它无法识别currentGame,因为您的范围内没有任何具有该名称的变量。

于 2013-09-03T02:23:01.110 回答
1

在该代码点的范围内没有任何名为“currentGame”的类型的变量。

此外,虽然 boardSize 是一个静态包保护变量,但方法 getBoardSize() 是一个实例变量。一种可能的解决方案是使方法静态和包保护,然后你可以这样做:

public void play() {
    this.boardSize = Game.getBoardSize();
}
于 2013-09-03T02:13:51.393 回答
1

这就像在 1 个函数中初始化一个 int 变量并尝试从另一个函数访问它。

对象(您尝试访问)超出了您尝试访问的代码部分的范围。 您可以通过将其作为参数发送并在相应方法中将其作为对象

接收来解决此问题。this

于 2013-09-03T02:21:50.400 回答
1

我们只能使用类引用来调用静态方法。因此,您可以使播放成为静态方法。

class Engine {

    int boardSize;

    Engine() {
    }

    public void play() {

        this.boardSize = currentGame.getBoardSize(); // *****1 Error is here.
        // *****It doesn't recognize currentGame, but I want it to.
    }

    static void doNothing() {
    }
}

class Board {

    Board() {
    }

    void Test() {
        Engine.doNothing();
    }
}

另一种方法是从类中创建一个对象并访问该对象中的非静态方法。

class Engine {

    int boardSize;

    Engine() {
    }

    public void play() {

        this.boardSize = currentGame.getBoardSize(); // *****1 Error is here.
        // *****It doesn't recognize currentGame, but I want it to.
    }

    void doNothing() {
    }
}

class Board {

    Board() {
    }

    void Test() {
        Engine gameEngine = new Engine();
        gameEngine.doNothing();
    }
}
于 2013-09-03T02:25:53.763 回答