java - 当参数超出范围时,如何在另一个类中创建对象?

标签 java object maze

我有三个类:MazesolverHexagonMaze。当我尝试在 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/

相关文章:

java - Toast 在 Android 4.3 中不起作用

java - 小数格式四舍五入到小数点后 4 位

c - 如何优化我的递归迷宫求解器?

java - LINQ的类似功能

java - Spring 可以自动将 HashMap 中的值绑定(bind)到数据持有者吗?

java - 如果我想根据不同的属性以不同的方式搜索一个对象,应该使用什么数据结构?

python - 为什么迷宫求解器代码不起作用

java - switch 语句和用户输入

javascript - 在 JS 中查找对象特定值的键

javascript - Underscore.js 按字母数字顺序对对象数组进行排序