I have three classes that need to communicate with eachother: a MainClass
, which will be the main running line; a Maze
class, which will contain MazeObject
s; and a MazeObject
class, which will populate the maze. In my MainClass
, I am creating a Maze object Maze maze = new Maze()
, which will populate it with walls in its constructor. I am then trying to fill the maze with MazeObjects, from the same MainClass
, by calling the MazeObject
class, which has methods to do so. So my question is, how am I supposed to give the MazeObject
class a copy of the maze so that it can populate it with maze objects?
我有三个需要与彼此通信的类:一个MainClass,它将是主要的运行线;一个Maze类,它将包含MazeObjects;和一个MazeObject类,它将填充迷宫。在我的MainClass中,我正在创建一个Maze对象Maze maze = new Maze(),它将在其构造函数中填充墙。然后我尝试通过调用MazeObject类来填充迷宫与MazeObjects,来自同一个MainClass,MazeObject类具有这样做的方法。所以我的问题是,我怎么能给MazeObject类一个迷宫的副本,以便它可以用迷宫对象填充它?
public class MainClass {
public static void main(String[] args){
Maze maze = new Maze(); //This creates the new maze object
MazeObject mazeObject = new MazeObject() //Populates the maze with tons of different types of maze objects
How am I supposed to implement the MazeObject class so that it will be able to take the newly created maze in order to populate the maze?
我该如何实现MazeObject类,以便它能够采用新创建的迷宫来填充迷宫?
1 个解决方案
#1
4
To do that, you can make a constructor that takes the maze like this:
要做到这一点,你可以创建一个像这样迷宫的构造函数:
public static void main(String[] args){
Maze maze = new Maze();
MazeObject mazeObject = new MazeObject(maze);
}
Maze object will have a reference to Maze like this
迷宫对象会像这样引用迷宫
class MazeObject {
private Maze maze;
public MazeObject(Maze maze) {
this.maze = maze;
}
}
#1
4
To do that, you can make a constructor that takes the maze like this:
要做到这一点,你可以创建一个像这样迷宫的构造函数:
public static void main(String[] args){
Maze maze = new Maze();
MazeObject mazeObject = new MazeObject(maze);
}
Maze object will have a reference to Maze like this
迷宫对象会像这样引用迷宫
class MazeObject {
private Maze maze;
public MazeObject(Maze maze) {
this.maze = maze;
}
}