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

时间:2016-10-18 04:19:56

标签: java object maze

我有三个课程:MazesolverHexagonMaze。当我尝试在Hexagon类中创建Mazesolver对象时,会发生错误。任何人都可以帮我解决这个问题吗?此外,在迷宫中获得对开始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
        }

2 个答案:

答案 0 :(得分:3)

我不是编码大师,但可能是因为唯一的Hexagon构造函数要求您传递HexType值。我可能错了,但我认为问题是,当t不是t值时,您将HexType传递给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的新手,请告诉我,如果我错了,我可以删除我的答案或至少更正。

答案 1 :(得分:0)

        Hexagon Start = new Hexagon(t);  //error

进入上面一行是正确的,除了:

1)可能 t 未定义,在使用之前首先将 t 定义为Hexagon。

2)可能是您已将 t 定义为代码,但必须为Hexagon,将其类型更改为Hexagon。

3)如果你在两个点之上都做得正确,那么你必须将Hexagon类导入MazeSolver。 lke:

import <Package>.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;
            HexType t;   // Change Here 
            Hexagon Start = new Hexagon(t);  // No Error