在Java中将字符串转换为类型
我有一门课:在Java中将字符串转换为类型,java,Java,我有一门课: public Account(String name, String passwd) { this.name = name; this.passwd = passwd; } public String getName() { return name; } public String getPasswd() { return passwd; } private String name; private String passwd; 我还上了这门
public Account(String name, String passwd)
{
this.name = name;
this.passwd = passwd;
}
public String getName()
{
return name;
}
public String getPasswd()
{
return passwd;
}
private String name;
private String passwd;
我还上了这门课:
public class CreateAccountAction implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
Account newAccount = new Account(GameGUI.textField.getText(), GameGUI.passwordField.getText());
Logger.send("You have created an account with the username, "+newAccount.getName()+".");
}
}
现在,我还有一节课:
public class LogInAction implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
String name = GameGUI.textField.getText();
if(GameGUI.passwordField.getText().equalsIgnoreCase(name.getPasswd()))
{
Logger.send("You have logged in.");
loggedIn = true;
}
else
{
Logger.send("Incorrect password.");
}
}
public boolean loggedIn()
{
return this.loggedIn;
}
private boolean loggedIn = false;
}
所以。在第三个类中,我尝试在第一个类中使用
name.getPasswd()
函数。但是,名称是一个字符串。但是我需要用字符串本身的名称引用对象。我创建了一个帐户leviathan=新帐户(“leviathan”,“password”)
。而name
字符串包含leviathan
。如何将该字符串的内容转换为帐户,leviathan?就像我在您对该程序的理解出现错误之前告诉您的那样。
实际上你想做什么
String name = GameGUI.textField.getText();
if(GameGUI.passwordField.getText().equalsIgnoreCase(name.getPasswd()))
但这不起作用,因为名称被声明为字符串而不是帐户,所以您不能为此请求getPasswd()方法
要纠正这个错误,您可以尝试在LogInAction中创建一个方法,例如
public Account getAccountFromName(String name)
{
// your code to get the account
}
你的代码会随着时间的推移而改变
String name = GameGUI.textField.getText();
Account acc = getAccountFromName(name);
if(GameGUI.passwordField.getText().equalsIgnoreCase(acc.getPasswd()))
但对于所有这些,您必须有一个现有帐户的arrayList,例如。
为此,您必须在CreateAccountAction中更改代码。例如,您需要添加帐户列表等。您将无法实现您正在尝试的目标。您需要创建一个服务来存储和检索来自某些数据源的帐户。然后,您可以调用该服务并按名称加载要查找的帐户 会有点像这样:
@Override
public void actionPerformed(ActionEvent arg0)
{
AccountService accountService = new AccountService();
Account account = accountService.getAccountByName(GameGUI.textField.getText());
if (account == null) {
Logger.send("Account name does not exist.");
} else {
if(GameGUI.passwordField.getText().equalsIgnoreCase(account.getPasswd()))
{
Logger.send("You have logged in.");
loggedIn = true;
}
else
{
Logger.send("Incorrect password.");
}
}
}
另外,作为提示,在存储登录凭据时使用哈希算法。这将在密码位于数据源中时保护密码。当用户登录时,只需对他们试图登录的密码进行哈希运算,然后将其与存储在数据源中的已哈希运算的密码进行比较。有许多方法可以实现您想要的。通过将每个actionlistener放在自己的类中,您已经让自己变得更难了 您从未将“newAccount”变量保存到任何位置,因此它只存在到您的ActionExecuted结束。要使用GUI处理任何类型的数据,您应该考虑创建一个新类,并将其称为控制器,并将数据保存在其中。然后可以在控制器中使用ArrayList作为变量,它是当前已知用户的列表(帐户对象的列表)。我们叫它accountList吧 然后,此控件类具有以下方法:
public static Account getAccountByName(String name){
// do some looping to find the account
}
public static boolean checkIfNameAlreadyExists(){
// make for-loop to check if name is in use
}
public static void addAccount(String name, String passwd){
accountList.add(new Account(name, passwd));
}
,并让构造函数在创建时创建一个新的ArrayList accountList(或者可能从文件中加载它们?)。然后,您的所有类都可以通过调用Control.addAccount(bla1,bla2)或Control.getAccountByName(“John Prescott”)来访问这些方法。
Joe提到的关于密码散列的要点在这里当然也是有效的。嗨,我想你搞错了你能做的。name.getPasswd()将永远不会工作,因为name被声明为字符串而不是帐户,因此您不能在此问题上请求getPasswd()方法。