从不同的类重用变量?

时间:2013-09-25 18:52:41

标签: java

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

如何从类文件中重用它:

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;
    }
}

这是我的世界级(我想重用变量的类)。

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 个答案:

答案 0 :(得分:0)

您需要引用要使用的类的实例。您需要访问要在该类中使用的字段。 e.g。

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);
}

答案 1 :(得分:0)

阅读encapsulationgetters 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();

答案 2 :(得分:0)

在课堂上为其添加一个getter并公开

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));

在构造函数之外,make randomNumber是类CiytWalls的成员变量

其次,将吸气剂添加到CityWalls

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

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

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

int rn = cw.getRandomNumber();

答案 3 :(得分:0)

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

希望这澄清了你的疑问。