【发布时间】:2013-02-14 22:58:19
【问题描述】:
这是我的代码。
private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {
String username = "";
String sql = "select * from userinfo where uname=? and pword=?";
try{
pst = conn.prepareStatement(sql);
pst.setString(1, txt_username.getText());
pst.setString(2, txt_password.getText());
rs=pst.executeQuery();
if(rs.next()){
afterLogin pwcorrect = new afterLogin();
pwcorrect.setVisible(true);
this.setVisible(false);
username = txt_username.getText();
}
else{
JOptionPane.showMessageDialog(null, "Username and Password are Incorrect.");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
我需要从另一个类访问用户名的值。有什么办法吗???请帮我写代码。
【问题讨论】:
-
定义公共访问器方法并使用它们。
-
我在你的代码中看到 no 私有类,我看到的只是一个带有局部变量用户名的私有方法。
-
@PremGenError:我很抱歉。你能告诉我如何从其他类或同一类中的其他方法访问这个局部变量用户名吗?能否请您也提供代码。
-
@user2068452 忘记其他类的,同一类中的其他方法将无法访问此本地 var 用户名。局部变量只限于声明它们的方法。这里更好的选择是使其成为私有实例变量,并具有公共 setter 和 getter 方法,其他类可以通过该方法访问用户名。
标签: java class private accessor