我有三个类:Mazesolver
、Hexagon
和 Maze
。当我尝试在 Mazesolver
类中创建一个 Hexagon
对象时,出现错误。谁能帮我解决这个问题?另外,在迷宫中获取对 Start Hexagon
的引用是什么意思?
public class Hexagon extends HexComponent
{
// constants
private static final Color WALL_COLOR = Color.BLACK;
private static final Color START_COLOR = Color.GREEN;
private static final Color END_COLOR = Color.YELLOW;
private static final Color UNVISITED_COLOR = Color.CYAN;
private static final Color PROCESSED_COLOR = Color.BLUE;
private static final Color PUSHED_COLOR = Color.MAGENTA;
private static final Color END_PROCESSED_COLOR = Color.RED;
private static final Color START_PROCESSED_COLOR = Color.PINK;
//enum to represent available hexagon types
public static enum HexType{WALL, START, END, UNVISITED, PROCESSED, PUSHED, END_PROCESSED, START_PROCESSED};
// Attributes
private HexType type; // Stores the type of Hexagon this currently is
private boolean isStart; // Is this the start?
private boolean isEnd; // Is this the end?
private Hexagon[] neighbors; // Stores the hexagons which surround this one on each of 6 sides
/**
* Create a Hexagon tile of the specified type
* @param t the HexType to create
*/
public Hexagon(HexType t) {
this.type = t;
this.isStart = t == HexType.START;
this.isEnd = t == HexType.END;
//set the initial color based on the initial type
this.setColor(this.type);
//allocate space for the neighbor array
this.neighbors = new Hexagon[6];
}
如何在 MazeSolver 中创建 Hexagon 对象?
public class MazeSolver
{
public static void main (String[] args) {
try {
if (args.length < 1) {
throw new IllegalArgumentException("No Maze Provided");
}
String maze0 = args[0];
private ArrayStack<String> steps;
Hexagon Start = new Hexagon(t); //error
}
最佳答案
我不是编码大师,但这可能是因为唯一的 Hexagon
构造函数要求您传递 HexType
值。我可能错了,但我认为问题在于,当 t
不是 HexType
值时,您将 t
传递给 Hexagon 构造函数.您需要将其中之一传递给 Hexagon 构造函数:HexType.WALL、HexType.START、HexType.END、HexType.UNVISITED、HexType.PROCESSED、HexType.PUSHED、HexType.END_PROCESSED、HexType.START_PROCESSED
.
编辑:是的,我认为可以肯定地说,您只需将 HexType.VALUE
传递给您的 Hexagon 构造函数,VALUE 是您的 HexType 枚举类的任何值。
我是 StackOverFlow 的新手,如果我错了请告诉我,这样我可以删除我的答案或至少更正它。
关于java - 当参数超出范围时,如何在另一个类中创建对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40099442/