2016-05-06 61 views
1

我想操作在main方法中聲明的名爲「players」的數組。我想在我的課堂中使用名爲「Glucksspielthread」的「玩家」 我知道我不能訪問「玩家」,因爲它是在主要方法中聲明的,並且對於其他類是不可見的。如何訪問另一個類的數組

我該如何解決這個問題?這裏是我的代碼:

import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 

public class Glucksspieltest { 
    public static void main(String[] args) { 
     int numPlayers = Integer.parseInt(args[0]); 
     int threadSize = Integer.parseInt(args[1]); 

     ExecutorService es = Executors.newFixedThreadPool(threadSize); 
     Glucksspielthread[] players = new Glucksspielthread[numPlayers]; 
     for (int i = 0; i < numPlayers; i++) { 
      players[i] = new Glucksspielthread(i); 
      es.execute(players[i]); 
     } 
    } 
} 

class Thinker { 
    public static void think(int Millisekunden) { 
     try { 
      Thread.sleep(Millisekunden); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void randomThink(int minMillisekunden, int  maxMillisekunden) { 
     System.out.println("test"); 
    } 
} 

class Glucksspielthread implements Runnable { 
    public int playerNumber; 

    Glucksspielthread(int number) { 
      playerNumber = number; 
    } 

    @Override 
    public void run() { 
     for (int i = 0; i <= playerNumber; i++) { 
      // here, I want to operate on array called "players" that is declared in the main method 
     } 
    } 
} 
+2

使其成爲公共和靜態 – dumitru

+0

只需在聲明您的類後將該數組設置爲變量字段,並且您可以在Glucksspieltest中的任何位置使用它。 – Lefteris008

+0

我想在課堂上使用它「Glucksspielthread」 – Blnpwr

回答

1

只是爲了測試目的,讓你的球員靜態變量和公衆在Glucksspieltest類,像這樣:

public class Glucksspieltest { 

public static Glucksspielthread[] players; 

然後存取權限是在這樣的Glucksspielthread類:

for (int i = 0; i <= playerNumber; i++) { 
     // here, I want to operate on array called "players" that is declared in the main method 
     Glucksspieltest.players 
    } 
+0

非常感謝,但是當我嘗試這樣做:for(int i = 0; i <= playerNumber; i ++){Glucksspieltest.players [i] = 10;} 他拋出一個錯誤「無法從int轉換成Glucksspielthread」 – Blnpwr

+1

更好的方式使它成爲'玩家'私人而不是公開的,並通過Glucksspieltest.getterPlayer()訪問它 –

+0

@ArvindGangwar它是正確的生產代碼,因爲我只提到測試/ – dumitru

1

添加方法Glucksspieltest類,並讓玩家陣列全球:

public class Glucksspieltest { 
    private static Glucksspielthread[] players; 

    public static Glucksspielthread[] getPlayers(){ 
     return players; 
    } 

    public static void main(String[] args) { 
     int numPlayers = Integer.parseInt(args[0]); 
     int threadSize = Integer.parseInt(args[1]); 

     ExecutorService es = Executors.newFixedThreadPool(threadSize); 
     players = new Glucksspielthread[numPlayers]; 
     for (int i = 0; i < numPlayers; i++) { 
      players[i] = new Glucksspielthread(i); 
      es.execute(players[i]); 
     } 
    } 
} 

這樣你就可以通過調用getPlayers()方法來獲得數組。

(需要注意的是,它會被勸添加一個構造函數來初始化並填寫players陣列,並從main方法分離機管理爲好。)

0

讓玩傢俬有全球全球化志願服務青年變量

public class Glucksspieltest { 
//Make a Global reference variable players 
    private static Glucksspielthread[] players; 

// Make a getter Method to get players 
public static Glucksspielthread[] getPlayers(){ 
    return players; 
} 

    public static void main(String[] args) { 
     int numPlayers = Integer.parseInt(args[0]); 
     int threadSize = Integer.parseInt(args[1]); 

     ExecutorService es = Executors.newFixedThreadPool(threadSize); 

     players = new Glucksspielthread[numPlayers]; 
     for (int i = 0; i < numPlayers; i++) { 
      players[i] = new Glucksspielthread(i); 
      es.execute(players[i]); 
     } 
    } 
} 

並通過Glucksspieltest.getPlayers()訪問它;

class Glucksspielthread implements Runnable { 
    public int playerNumber; 

    private static Glucksspielthread[] players; 

    Glucksspielthread(int number) { 
      playerNumber = number; 
    } 

    @Override 
    public void run() { 
     for (int i = 0; i <= playerNumber; i++) { 
      // here, I want to operate on array called "players" that is declared in the main method 
     players= Glucksspieltest.getPlayers(); // play with players 
     } 
    } 
} 
+0

當我希望每個單一的玩家該陣列將獲得10美元的發揮。 Glucksspieltest.getplayers [i]不會工作。我如何操作「播放器」的數組字段? – Blnpwr