-1

基本上我让 I​​ 类将网格扩展到 11 x 11,构造函数被调用,但它没有改变网格。任何有关原因的输入或信息将不胜感激。我在下面发布了我的代码:

import info.gridworld.actor.*;
import info.gridworld.grid.BoundedGrid;

public class ChangeGrid extends ActorWorld{

private static final int SIZEROW = 11;
private static final int SIZECOL = 11;

public ChangeGrid()
{
    super(new BoundedGrid<Actor>(SIZEROW, SIZECOL));
    System.out.println("test");
}

}


import info.gridworld.actor.Bug;
public class XBug extends Bug {
    /**
     * A <code>BoxBug</code> traces out a square "box" of a given size. <br />
     * The implementation of this class is testable on the AP CS A and AB exams.
     */

        private int steps;
        private int sideLength;
        private int x = 0;

        private static ChangeGrid object = new ChangeGrid();


        /**
         * Constructs a box bug that traces a square of a given side length
         * @param length the side length
         */
        public XBug(int length)
        {
            steps = 0;
            sideLength = length;
        }

        /**
         * Moves to the next location of the square.
         */
        public void act()
        {
            if (steps < sideLength && canMove())
            {
                if(x==0)
                {
                    turn();
                }
                move();
                steps++;
                x++;
            }
            else
            {
                turn();

                steps = 0;
            }
        }

}
4

1 回答 1

0

创建世界时不会调用您的构造函数。您在类中调用它Actors,然后您甚至不调用该.show()方法。您只需要将跑步者课程更改为 callChangeWorld而不是ActorWorld.

这是您的代码的示例。

    public class Runner 
    {
        public static void main(String[]args)
        {
            ChangeWorld world = new ChangeWorld();
            XBug bug = new XBug();

            world.add(bug);

            world.show();
        }
    }    
于 2014-04-04T21:05:46.317 回答