0

这可能很简单,但我只是不知道该怎么做?

我如何从类文件中重用它:

int randomNum = 5 + (int)(Math.random() * ((10 - 5) + 1));

然后使用在另一个类文件中获得的相同结果?

我的 CityWall 类(包含我要使用的 int 的类。)

public class CityWalls extends Thing {

    int randomNum = 5 + (int)(Math.random() * ((10 - 5) + 1));

    public CityWalls(City c, int st, int av, Direction d) {
        super(c, st ,av ,d);

        int oddIncrement = 0;
        if (randomNum % 2 == 0)
        {
            oddIncrement = 1;
        }

        for (int i = 0; i < randomNum; i++) {
            new Wall(c, i+(7-randomNum/2), (7-randomNum/2), Direction.WEST);
            new Wall(c, i+(7-randomNum/2), (7+randomNum/2) - oddIncrement, Direction.EAST);
            new Wall(c, (7-randomNum/2), i+(7-randomNum/2), Direction.NORTH);
            new Wall(c, (7+randomNum/2)-oddIncrement, i+(7-randomNum/2), Direction.SOUTH);
        }
    }

    public int getRandomNum() {
        return randomNum;
    }
}

这是我的 World Class(我想重用变量的 Class)。

public class World {

    public static void main (String[] args) {
        int height = 0, width = 0, top = 5, left = 5, thingCount = 20;
        World b = new World();
        Random rand = new Random();
        // int RandomNumber = rand.nextInt(9);

        CityWalls cw = new CityWalls(Gothenburg, 5 , 5, Direction.NORTH); 
        int RandomNumber = cw.getRandomNum();

        height = (int)(Math.random() * 0.5) + RandomNumber; // I want to use the variable here.
        width = (int)(Math.random() * 0.5) + RandomNumber; // And here to replace the RandomNumber. then it should be correct.

        City Gothenburg = new City(16,16);


        Thing[] things = ThingSpawnCity(Gothenburg, width, height, top, left, thingCount);
        RobotFinder terminator = new RobotFinder(Gothenburg, 7, 7, Direction.NORTH, 0);

        terminator.run();
    }

    public static Thing[] ThingSpawnCity(City Gothenburg, int width, int height, int top, int left, int objects){
        Thing things[] = new Thing[objects];
        int avenue = 0, street = 0;

        for (int i = 0; i < objects; i++){
            avenue = (int)(Math.random() * width) + left;
            street = (int)(Math.random() * height) + top;
            things[i] = new Thing(Gothenburg, street, avenue);
        }

        return things;
    }
}
4

4 回答 4

0

您可以通过将成员变量randomNum作为类CityWalls的静态变量来做到这一点。并将该变量用于您的World类。通过这样做,您将获得与CityWalls类中指定的值相同的值。但是您实际上想要重用,或者我会说使用具有相同名称的变量并且不想为它分配内存或您发现它对您的案例有用的任何其他原因,那么您应该将CityWalls类扩展到您的World类。

希望这能澄清你的疑问。

于 2013-09-25T19:30:13.067 回答
0

您需要对要使用的类的实例的引用。您需要访问要在该类中使用的字段。例如

class A {
    int randomNum = 5 + (int)(Math.random() * ((10 - 5) + 1));
}

class B {
    public void printNum(B b) {
        System.out.println("Random Num " + b.randomNum);
    }
}

public static void main(String... ignored) {
    A a = new A();
    B b = new B();
    b.printNum(a);
}
于 2013-09-25T18:55:10.037 回答
0

阅读encapsulationand getters and setters,你可以这样做:

int randomNum = 5 + (int)(Math.random() * ((10 - 5) + 1));
MySuperOtherClass other = new MySuperOtherClass();
other.setX(randomNum);
// now you can get it back with other.getX();
于 2013-09-25T18:55:17.367 回答
0

在您的班级中为其添加一个吸气剂并将其公开

public int getRandomNum() {
    return randomNum;
}

编辑

包含您的随机数的类:

public class ClassHavingARandomNumner {
    int randomNum = 5 + (int)(Math.random() * ((10 - 5) + 1));

    public int getRandomNum() {
        return randomNum;
    }
}

另一个想要访问随机数的类

public class ClassWantingARandomNumber {
    public static void main(String[] args) {
        // create an instance of the class containing the random number
        ClassHavingARandomNumner c = new ClassHavingARandomNumner();
        // call the getter method to retrieve the random number
        System.out.println(c.getRandomNum());
    }
}

编辑 2

第一步

int randomNum = 5 + (int)(Math.random() * ((10 - 5) + 1));

在构造函数之外创建randomNumber类的成员变量CiytWalls

二、将getter添加到CityWalls

然后在 main 中将构造函数调用的结果分配给局部变量

CityWalls cw = new CityWalls(Gothenburg, 5 , 5, Direction.NORTH);

之后,您可以使用以下方法访问您创建的 CityWalls 对象的随机数:

int rn = cw.getRandomNumber();
于 2013-09-25T18:55:55.273 回答