2013-09-25 38 views
0

這可能很簡單,但我不知道該怎麼做?重用不同類的變量?

我如何重用這個類文件中:

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

爲了然後用同樣的結果,這在採購其他類文件?

我的城牆上類(含我想用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; 
    } 
} 
+0

@ A4L現在編輯! – User999

+0

看到我的第二個編輯! – A4L

+0

你認爲你可以通過編輯將它們放入我的代碼中嗎?我更好地學習,因爲那樣我就會更容易理解。如果對你來說當然不是很麻煩! – User999

回答

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

我不明白我有兩個單獨的文件,並在其中一個文件我有randomNum變量,我想或多或少轉移/重複使用與其他文件中的相同結果。如果有幫助,我可以發佈我的整個代碼? – User999

+0

@WilliamBergendahl當你的程序運行時,你沒有文件。你有類的實例。對於一個實例在另一個實例中訪問值,它必須有一個對它的引用。 –

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

添加一個getter它在你的類並予以公佈

public int getRandomNum() { 
    return randomNum; 
} 

編輯

Class co ntaining您的隨機數:

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

EDIT 2

第一招

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

外另一類使randomNumber成員va類CiytWalls

二的可變結構,吸氣劑添加到CityWalls

然後在主構造函數調用的結果後分配到一個局部變量

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

可以存取權限的隨機數您創建的CityWalls對象使用:

int rn = cw.getRandomNumber(); 
+0

嘗試「randomNum無法解析爲變量」時收到錯誤。 – User999

+0

@WilliamBergendahl,顯然你已經把這個方法放在一個不是包含'int randomNum = 5 +(int)(Math.random()*((10-5)+ 1));'的類中。看我的編輯。 – A4L

+0

我編輯併發布我的代碼,看看您是否可以更好地理解並幫助我放置代碼的位置。 @ A4L – User999

0

您可以通過將成員變量randomNum作爲類的靜態變量CityWalls。並使用該變量到您的世界類。通過這樣做,您將獲得與CityWalls類中指定的值相同的值。但你實際上想要重用或我會說使用相同名稱的變量&不想爲它分配內存或任何其他原因,你會發現它對你的情況有用,那麼你應該擴展CityWalls class to your 世界類。

希望這可以澄清你的疑問。