2014-02-16 136 views
-2

我有一個私人無效類,這是我的按鈕,該按鈕從數據庫中獲取一些resutls,我需要這些變量在另一個私人類中使用,我應該怎麼做? 抱歉,如果我錯了,我是一個新手,請幫助...訪問私有類變量Java

private void searchActionPerformed(java.awt.event.ActionEvent evt) {          
    String search_updt= search_username_fld.getText(); 

    try{ 
     Connection con = DriverManager.getConnection(host,dbname,dbpass); 
     Statement log = (Statement) con.createStatement(); 
     //search for the user in database 
     ResultSet rs_log = log.executeQuery("SELECT * FROM Users WHERE Username='"+search_updt+"'"); 

     while(rs_log.next()){ 
      String rs_user = rs_log.getString("username"); 
      String rs_pass = rs_log.getString("password"); 
      String rs_first = rs_log.getString("first_name"); 
      String rs_last = rs_log.getString("last_name"); 

      //if authentication for user successful, go to user panel 
      if(rs_user.equals(search_updt)){ 
       update.setVisible(false); 
       updating.setVisible(true); 
       this.setSize(430, 391); 
      } 

      //else show error 
      else { 
       JOptionPane.showMessageDialog(null, "Username Not Found!"); 
       break; 
      } 
     } 
    } 
    catch(SQLException | HeadlessException e){ 
     JOptionPane.showMessageDialog(null, e); 
    } 
} 

我需要訪問rs_user,rs_pass,...從另一個私有類

+3

*吸氣劑*和* * setter方法是你的朋友。閱讀[封裝](http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29)。 – Maroun

+1

「private void class」它們是什麼? – Batty

+0

你應該按照這個教程 - http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html –

回答

0

我認爲你應該考慮使用Observer模式,因爲您有其他類需要的局部變量,並且您的ResutSet由0個或多個記錄組成。

public class ResultSetEvent { 
    private String rs_user; 
    private String rs_pass; 
    // ... 

    public ResultSetEvent(String user, String pass) { 
     this.rs_user = user; 
     this.rs_pass = pass; 
    } 

    // getters/setters 

} 

public interface SearchActionResultListener { 
    void handleResult(ResultSetEvent event); 
} 

public class MyClass { 
    private List<SearchActionResultListener> listeners = new LinkedList<SearchActionResultListener>(); 
    public void addSearchActionResultListener(SearchActionResultListener listener) { 
     listeners.add(listener); 
    } 
    public void removeSearchActionResultListener(SearchActionResultListener listener) { 
     listeners.remove(listener); 
    } 
    //... 

    private void searchActionPerformed(java.awt.event.ActionEvent evt) { 
     //... 
     while(rs_log.next()){ 
      String rs_user = rs_log.getString("username"); 
      String rs_pass = rs_log.getString("password"); 
      //... 

      ResultSetEvent event = new ResultSetEvent(rs_user, rs_pass) 
      for(SearchActionResultListener l : this.listeners) { 
       l.handleResult(event); 
      } 
     } 
     //... 
    } 
} 

public class MyOtherClass implements SearchActionResultListener { 
    private MyClass reference; 

    public MyOtherClass() { 
     // reference = ... 
     reference.addSearchActionResultListener(this); 
     // ... 
    } 

    @Override 
    public void handleResult(ResultSetEvent event) { 
     // handle the event 
    } 
} 
-1

@ user3315606:你的類結構是不妥當的,但因爲你已經提到你的Java開發一個新的傢伙是可以接受的。 我給你詳細的:

所有的拳頭如果你想訪問一個類的任何變量到另一個類,那麼他們應該在類級別與私人訪問說明符,他們應該有關聯的公共getter和setter 。

下面是虛擬類根據您的要求:

public class Dummy{ 

    private String rs_user; 
    private String rs_pass; 
    private String rs_first; 
    private String rs_last; 

    public void setRs_user(String rs_user){ 
    this.rs_user = rs_user; 
    } 

    public void setRs_pass(String rs_pass){ 
    this.rs_pass = rs_pass; 
    } 

    public void setRs_first(String rs_first){ 
    this.rs_first = rs_first; 
    } 

    public void setRs_last(String rs_last){ 
    this.rs_last = rs_last; 
    } 

    public String getRs_user{ 
    return rs_user; 
    } 

    public String getRs_pass{ 
    return rs_pass; 
    } 

    public String getRs_first{ 
    return rs_first; 
    } 

    public String getRs_last{ 
    return rs_last; 
    } 

    private void searchActionPerformed(java.awt.event.ActionEvent evt) { 
    ... 
    setRs_user(rs_log.getString("username")); 
    setRs_pass(rs_log.getString("password")); 
    setRs_first(rs_log.getString("first_name")); 
    setRs_last(rs_log.getString("last_name")); 
    ... 
    } 
    ... 
} 

Now if you want to access rs_user, rs_pass ... in other classes then you can use getters method for these properties. 
Like fot rs_user you can use getRs_user() method on the object of Dummy class.