2012-11-09 182 views
0

我有以下代碼如何獲得一個類的實例?

class driver{ 
     static BankAccount GetAccount(Customer customer, char c) { 
      BankAccount accSrc = customer.S; 
      // savings account 
      if (c =='S') { 
       accSrc = customer.S; 
      // loan account 
      } else if (c =='L') { 
       accSrc = customer.L; 
      // checking account 
      } else if (c =='C') { 
       accSrc = customer.C; 
      // auto loan account 
      } else if (c =='A') { 
       accSrc = customer.A; 
      } 
      return accSrc; 
     } 
     public static void main(String[] args) { 
      // TODO Auto-generated method stub 
      Customer forrest = new Customer("Forrest Gump", 1, "42 New Street, New York, New York"); // me 
      Customer random = new Customer("Random Name", 2, "44 New Street, New York, New York"); // imaginary partner 
      //try{ 
       String input = JOptionPane.showInputDialog("Please enter your transaction information: "); 
       Scanner s = new Scanner(input); 
       int id = Integer.parseInt(s.next()); 
       char action = Character.toUpperCase((s.next().charAt(0))); 
       char accSrc = ' '; 
       char accDest = ' '; 
       double amount = 0; 

       if(action == 'T'){ 
        amount = s.nextDouble(); 
        accSrc = s.next().charAt(0); 
        accDest = s.next().charAt(0); 
       }else if(action == 'G' || action == 'I'){ 
        accSrc = s.next().charAt(0); 
       }else{ 
        //if D,W 
        amount = s.nextDouble(); 
        accSrc = s.next().charAt(0); 
       } 

      //}catch (IOException e){ 

      //} 
       if(id==1){ 
        return forrest; 
       }else if(id == 2){ 
        return random; 
       } 
       BankAccount src = GetAccount(forrest, accSrc); 
       System.out.print(src.getOwner().name); 
       if(action == 'T'){ 
        BankAccount dst = GetAccount(forrest, accDest); 
        src.transfer(amount, dst); 
. 

    .. 
    } 

    class Customer{ 
     protected String name; 
     protected int id; 
     protected String address; 
     protected BankAccount C = new BankAccount(id, this, 0); 
     protected BankAccount S = new BankAccount(id, this, 0); 
     protected BankAccount A = new BankAccount(id, this, 0); 
     protected BankAccount L = new BankAccount(id, this, 0); 
    ... 
    } 

目前即時通訊硬編碼的BankAccount src = GetAccount(forrest, accSrc);我如何可以繼續編寫代碼,以便它返回給定的ID號的客戶的一個實例(比如1倍中給出的回報阿甘,2倍中給出的回報隨機)?

回答

0

更好的

Customer customers[] = { forrest, random }; 

我可以打電話與customer[id]

0

您需要將您的東西存儲在List中,然後通過索引檢索列表項。

3

將你的課程放在Map例如

Map<Integer, Customer> classMap = new HashMap<Integer, Customer>(); 
Customer forrest = 
     new Customer("Forrest Stallings", 1, "42 New Street, New York, New York"); 
classMap.put(1, forrest); 

Customer random = 
     new Customer("Random Name", 2, "44 New Street, New York, New York"); 
classMap.put(2, random); 

然後,只需讓你的班級爲:

Customer forrest= classMap.get(1); 
Customer random = classMap.get(2); 
+0

實例,這是唯一的辦法? – user133466

+0

@ user133466根據您的需求,可能會有更多,但考慮到這種情況,這是最簡單的,並達到目的。 –

+0

@ user133466有很多方法,這確實是一個。您可以使用列表,數組,文件甚至數據庫。事實上,如果你仍然是硬編碼的東西,這是最簡單的解決方案。 – Gamb

相關問題