2013-11-26 181 views
-1

这是我的代码。 在我的主要功能中,我已经包含一个构造函数。 我有多个构造函数在我的客户端类有构造函数问题

public class Client { 
    private String name = "", username = "", ClientID = "", password = ""; 

    Client[] account = new Client[100]; 

    public Client() { 

    } 

    public Client(String name, String username) { 
     // will have generated function for ClientID and also Password 

     account[clientCount] = new Client(); 

     account[clientCount].setName(name); 
     account[clientCount].setUsername(username); 
     account[clientCount].setPassword(password); 
     account[clientCount].setClientID(CID); 
    } 

    public Client(String name, String username, String password, String ClientID) { 
     this.name = name; 
     this.username = username; 
     this.password = password; 
     this.ClientID = ClientID; 
    } 

    public static void main(String[] args) { 

     if (selection == 1) { 
      Client client = new Client(); // object created called "client" 

      name = JOptionPane.showInputDialog("Account Status: Admin\n" + "Please Enter Client Name: "); 

      username = JOptionPane.showInputDialog("Account Status: Admin\n" + "Please Enter Client Userame: "); 

      Client CLIENT = new Client(name, username); 

      JOptionPane.showMessageDialog(null, 
        CLIENT.account[Client.clientCount].getName() + "\n" + CLIENT.account[Client.clientCount].getUsername() + "\n" 
          + CLIENT.account[Client.clientCount].getPassword() + "\n" + CLIENT.account[Client.clientCount].getClientID()); 

     } 

     if (selection == 2) { 
      // at here, unable to access to CLIENT object, what can I do access 
      // CLIENT object to here. Or copy the same object into the other new 
      // object 

      for (int i = 0; i <= Client.clientCount; i++) { 
       System.out.println(CLIENT.account[i].getName()); 
      } 

     } 

    } 
} 
+2

而这个问题是......? – Keppil

+0

我无法访问“if(选择== 1)”之外的CLIENT对象 – JaxLee

+0

加入guys,wtf?重新说出Q并请格式化答案。啧。 – Shiki

回答

3

只要把你的Client CLIENT = null;如果块之前,然后使用CLIENT = new Client(name,username);的,如果里面。然后,您可以在您的if之外使用CLIENT,但如果第一个if中的代码未执行,则它将为null

其实你的程序有一个设计错误,因为CLIENT总是会在第二,如果null,因为它永远是第一,第二,如果块后海誓山盟执行;-)所以,你应该重新考虑你的情况问题。

+0

感谢您的评论。我会考虑的。 :) – JaxLee