如何调用在另一堂课中启动的2D数组?
我简化了我的问题的最小示例:迷宫
类用方法generateMaze()
(mazearray的内容
>在此示例中是无关紧要的)。 Walker
调用该方法的主线程,从而从Maze> Maze
类创建此mazearray
。
我不明白如何在walker.walk
中调用此数组?恐怕我有一个知识差距。
每个提示都非常感谢,非常感谢。
public final class Maze {
public static boolean[][] generateMaze(int width, int height) {
boolean[][] mazeArray = new boolean[width][height];
for( int x = 0; x < width; x++ ) {
mazeArray[x][0] = true;
}
for( int y = 0; y < height; y++ ) {
mazeArray[0][y] = true;
}
return mazeArray;
}
}
public class Walker {
public static void main(String[] args) {
Maze mazeObj = new Maze();
boolean[][] maze = Maze.generateMaze(2,2);
}
public void walk(Maze maze) {
// Traverse Array
}
}
I made a minimal reduced example of my problem: the Maze
class creates a 2D boolean array with the method generateMaze()
(the content of mazeArray
is irrelevant in this example). The main thread from Walker
calls that method and thereby creates this mazeArray
from the Maze
class.
I do not understand how I can call this array in Walker.walk
? I'm afraid I have a knowledge gap.
Every hint is appreciated, thank you very much.
public final class Maze {
public static boolean[][] generateMaze(int width, int height) {
boolean[][] mazeArray = new boolean[width][height];
for( int x = 0; x < width; x++ ) {
mazeArray[x][0] = true;
}
for( int y = 0; y < height; y++ ) {
mazeArray[0][y] = true;
}
return mazeArray;
}
}
public class Walker {
public static void main(String[] args) {
Maze mazeObj = new Maze();
boolean[][] maze = Maze.generateMaze(2,2);
}
public void walk(Maze maze) {
// Traverse Array
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
说明
这里有几个基本的OOP 错误。
首先,为什么当您的
Maze
类创建一个实例时,当您的generateMaze
类是static
,然后返回迷宫作为<代码>布尔值[] [] 而不是迷宫
。您可能打算将数组作为类的字段,而不是直接通过迷宫实例访问数组。接下来,
walk
方法是非静态的,并且是Walker
实例的一部分。因此,您需要创建该类的实例并在该实例上调用该方法。迷宫生成
您可能打算这样做:
呼叫
使用构造函数
或更好的
,使用构造函数:并在
main
中这样称呼它:工厂
您仍然可以将其与出厂方法相结合,如果您真的想要。但是创建逻辑应该在(可能
private
)构造函数中:尽管如此,请将其称为:
Walker
现在,您需要一个
Walker
类的实例,并在此上调用该方法,让它成为您刚刚产生的迷宫:Explanation
There are several basic OOP mistakes here.
First of all, why do you even create an instance of the
Maze
class when yourgenerateMaze
class isstatic
and returns the maze as instance ofboolean[][]
instead ofMaze
. You probably intended to have the array as a field of the class instead and not access the array directly but via a maze instance.Next, the
walk
method is non-static and part ofWalker
instances. So you would need to create an instance of that class and call the method on that instance.Maze generation
You probably intended to do this instead:
with a call like
Constructor
Or even better, use a constructor:
And call it like this in your
main
:Factory
You can still couple that with a factory method, if you really want. But the creation logic should be in a (possibly
private
) constructor nonetheless:calling it like:
Walker
Now, you need an instance of the
Walker
class and call the method on that, giving it the maze you just generated: