2014-10-09 81 views
0

我有一個命令行輸入爲0 1 2,主代碼可以在下面看到。將String [] args值傳遞給另一個類

public class AppleStoreRunner { 
    public static void main(String [] args) { 

     //maximum size of queue 
     int qCapacity = Integer.parseInt(args[0]); 

     //number of simulation hours 
     int simHours = Integer.parseInt(args[1]); 

     //average number of customers per hour 
     int custPerHour = Integer.parseInt(args[2]); 

     AppleStore myStore = new AppleStore(qCapacity, simHours, custPerHour); 

     //Run simulation 
     myStore.simulation(); 
     myStore.displayAcceptedCustomers(); 
     myStore.displayServedCustomers(); 
     myStore.displayWaitingCustomers(); 
     myStore.displayTurnAwayCustomers(); 

    } 
} 

如何在下面的類中調用輸入命令行參數,以便我可以在單獨的擴展類中使用輸入?下面的代碼是我試圖爲3個輸入數字創建變量的類。

public class AppleStore { 

int qCapacity; 
int simHours; 
int custPerHour; 

/** Constructor 
* @param qCapacity The initial capacity of the queue to be used. 
* @param simHours The number of hours that the simulation should run. 
* @param custPerHour expected number of customers to arrive per hour. 
*/ 
    public AppleStore(int qCapacity, int simHours, int custPerHour) 

    { 
     qCapacity = AppleStoreRunner.main(args); 
    } 
/** 
    * This methods performs a simulation of a store operation using a queue and prints the statistics. 
    * For every minute, the simulator 1) checks if there are new customers arriving; 2) adds the new customer into the waiting line or else records the customer who chooses to leave; 3) continues to help the current customer if the current customer is not finished yet, or else get the next person in the waiting line. The simulator starts at minute 0, and repeats every minute until it finishes the requested simulation time. 
    */ 
    public void simulation() 
    { 
    System.out.println("Average Waiting Time" +); 
    System.out.println("Average Line Length" +); 

    /** 
    * print the info of all accepted customers 
    */ 
    } 
     public void displayAcceptedCustomers() 
     { 
      System.out.println("Customers accepted" +); 

    /** 
    * print the info of all served customers 
    */ 
     } 
     public void displayServedCustomers() 

     /** 
     * print the info of all waiting customers 
     */ 
     public void displayWaitingCustomers() 

    /** 
    * print the info of all turned away customers 
    */ 
    public void displayTurnAwayCustomers() 

} 
+4

你想創建一個無限遞歸的程序嗎?我不明白這一點。你的'main'方法創建一個新的'AppleStore',它調用構造函數,調用'main',創建一個新的'AppleStore',這......如果你的目標是試圖通過使Apple Store崩潰堆棧溢出,我懷疑這將工作。 :) :) – ajb 2014-10-09 23:59:52

回答

5

因爲你在你的主要方法做了AppleStore myStore = new AppleStore(qCapacity, simHours, custPerHour);,所有你需要做的就是定義一個適當的構造函數。

public AppleStore(int qCapacity, int simHours, int custPerHour) 

{ 
    this.qCapacity = qCapacity; 
    this.simHours = simHours; 
    this.custPerHour = custPerHour; 
} 

當您將三個實例變量聲明爲package-private時,子類會自動看到三個變量的存在。

但是,如果你讓子類某種程度上不受更改到超(我的意思是,AppleStore),我建議增加一些干將三個變量比可以從子類調用,例如:

int getQueueCapacity() { 
    return this.qCapacity; 
} 

並將三個變量的訪問級別更改爲私有。

+0

非常感謝。我認爲我在main中的構造函數是私有的,因此假定需要一個新的構造函數。 – 2014-10-10 00:05:27