Java 重用来自不同类的变量?

Java 重用来自不同类的变量?,java,Java,这可能很简单,但我就是不知道怎么做 如何从类文件中重用此文件: int randomNum = 5 + (int)(Math.random() * ((10 - 5) + 1)); 然后使用在另一个类文件中获得的相同结果 My CityWall类(包含我要使用的int的类。) 公共类城市墙扩展了{ intrandomnum=5+(int)(Math.random()*((10-5)+1)); 公共城市墙(城市c、内街、内街av、方向d){ 超级(c、st、av、d); int=0; 如果(随机

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

如何从类文件中重用此文件:

int randomNum = 5 + (int)(Math.random() * ((10 - 5) + 1));
然后使用在另一个类文件中获得的相同结果

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

公共类城市墙扩展了{
intrandomnum=5+(int)(Math.random()*((10-5)+1));
公共城市墙(城市c、内街、内街av、方向d){
超级(c、st、av、d);
int=0;
如果(随机数%2==0)
{
ODD增量=1;
}
for(int i=0;i
这是我的世界类(我希望重用变量的类)

公共类世界{
公共静态void main(字符串[]args){
内部高度=0,宽度=0,顶部=5,左侧=5,厚度=20;
世界b=新世界();
Random rand=新的Random();
//int random number=rand.nextInt(9);
城市墙cw=新城市墙(哥德堡,5,5,北向);
int RandomNumber=cw.getRandomNum();
高度=(int)(Math.random()*0.5)+RandomNumber;//我想在这里使用变量。
宽度=(int)(Math.random()*0.5)+RandomNumber;//在这里替换RandomNumber。那么它应该是正确的。
哥德堡市=新城(16,16);
Thing[]things=ThingSpawnCity(哥德堡,宽度,高度,顶部,左侧,thingCount);
机器人搜寻器终结者=新的机器人搜寻器(哥德堡,7,7,Direction.NORTH,0);
terminator.run();
}
公共静态事物[]ThingSpawnCity(哥德堡市,整数宽度,整数高度,整数顶部,整数左侧,整数对象){
事物事物[]=新事物[物体];
int大道=0,街道=0;
for(int i=0;i
您需要对要使用的类的实例的引用。您需要访问要在该类中使用的字段。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);
}

阅读一下
封装
获取者和设置者
,您可以这样做:

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

在你的课堂上为它添加一个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));
在构造函数之外,使
randomNumber
成为类
CiytWalls

其次,将getter添加到
CityWalls

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

CityWalls cw = new CityWalls(Gothenburg, 5 , 5, Direction.NORTH);
之后,您可以访问使用以下方法创建的CityWalls对象的随机数:

int rn = cw.getRandomNumber();

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


希望这澄清了您的疑问。

我在尝试“randomNum无法解析为变量”时收到错误。@WilliamBergendahl,显然您已将该方法放置在一个类中,而该类不包含
int randomNum=5+(int)(Math.random()*((10-5)+1))。查看我的编辑。我编辑并发布我的代码,看看您是否能更好地理解并帮助我放置代码@A4LI不明白我有两个独立的文件,其中一个文件中有randomNum变量,我希望或多或少地在另一个文件中传输/重用该变量,并获得相同的结果。如果有帮助的话,我可以发布我的全部代码?@WilliamBergendahl当你的程序运行时,你没有文件。您有类的实例。一个实例要访问另一个实例中的值,它必须有对它的引用。@A4L立即编辑!你认为你可以通过编辑把它们放到我的代码中吗?那样我会学得更好,因为那样我会更容易理解。当然,如果这对你来说不太麻烦的话!CityWalls构造函数中for循环的实际意义是什么?@A4L我已经按照你说的做了,但是我现在进入了我的世界级,int rn=cw.getRandomNum();“类型CityWalls的getRandomNum()方法未定义”它复制墙,使高度和长度更长
CityWalls cw = new CityWalls(Gothenburg, 5 , 5, Direction.NORTH);
int rn = cw.getRandomNumber();