简体   繁体   English

如何访问局部变量,该局部变量是另一个类中的对象? -Java

[英]How can I access a Local Variable which is an object from within another class? - Java

My understanding is that you can't do what I'm asking. 我的理解是,您无法做到我的要求。 If you look at the starred ( * ) commented errors in the code below, you can see what I'm trying to access. 如果您在下面的代码中看到加星号( * )的注释错误,则可以看到我要访问的内容。 I feel like I need to be able to do this so that I can use a method to dynamically create many objects and then access all of those objects from other objects. 我觉得我需要能够做到这一点,以便可以使用一种方法动态创建许多对象,然后从其他对象访问所有这些对象。

Is there a way to do this that I'm missing, or am I just messing something up? 有没有办法做到这一点,或者我只是搞砸了? If not, how should I go about doing this to enable me to get the same functionality as below? 如果没有,我应该如何做才能使我获得以下相同的功能? If there's any way to do this other than passing the objects around, it would be appreciated (passing objects seems like so much work - especially with multi-dimensional arrays of objects - there should be an easy way to instantiate package-private objects that can be accessed anywhere else in the package). 如果除了传递对象之外还有其他方法可以做到,那就不胜感激(传递对象似乎需要做很多工作-尤其是对于多维对象数组-应该有一种简便的方法来实例化可以使程序包私有的对象可以在包中的其他任何地方访问)。 But if passing is the only way, please let me know the best way to do it, especially when I'm passing a two-dimensional array of a bunch of objects. 但是,如果传递是唯一的方法,请让我知道执行此操作的最佳方法,尤其是当我传递一堆对象的二维数组时。 Thanks! 谢谢!

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

You will get access to gameEngine through your Board class by passing it as a parameter to Board . 您将可以访问gameEngine通过你的Board通过将其作为参数传递给类Board When you instantiate your Board , you could do something like this: 实例化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
    }
}

Take a look at the concept of message-driven communication. 看一下消息驱动的通信的概念。 Things might get clearer for you by reading this answer . 阅读此答案可能会使事情变得更清楚。

In the following picture, which I took from the answer linked above, you can imagine f as your engine object within the Engine class, and c as your engine within the Board class. 在下面的图片中,我从上面的链接中得到答案,您可以将f想象为Engine类中的引擎对象,将c想象为Board类中的引擎 You are actually manipulating the same object. 您实际上是在操纵同一对象。

在此处输入图片说明

As for your other problem (the first one): it can't recognize currentGame because you don't have any variable with that name in your scope. 至于另一个问题(第一个问题):它无法识别currentGame因为您的作用域中没有任何具有该名称的变量。

There is no variable of any type named 'currentGame' in scope at that point in the code. 在代码的那一点上,在作用域中没有任何名为“ currentGame”的变量。

Furthermore, while boardSize is a static package-protected variable, the method getBoardSize() is an instance variable. 此外,虽然boardSize是受程序包保护的静态变量,但方法getBoardSize()是实例变量。 One possible solution is to make the method static and package protected, then you can do this: 一种可能的解决方案是使该方法成为静态方法并受程序包保护,然后可以执行以下操作:

public void play() {
    this.boardSize = Game.getBoardSize();
}

This is like initializing an int variable in 1 function and trying to access it from another function. 这就像在1个函数中初始化一个int变量并尝试从另一个函数访问它一样。

The objects(you are trying to access) are out of scope in the part of the code where you are trying to access. 对象(您要访问的对象)超出了您要访问的代码部分的范围。

You can resolve this issue by sending this as a parameter and recieving it as an object in the corresponding method. 您可以通过将this作为参数发送并在相应方法中将其作为对象接收来解决此问题。

We can use class reference to call static methods only. 我们只能使用类引用来调用静态方法。 So you can make the play a static method. 这样您就可以使该播放成为静态方法。

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

The other way is to make an Object from the class and access the non static methods within that object. 另一种方法是从类中创建一个Object并访问该对象中的非静态方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM