现在我有一个运行银行应用程序的单一程序。
我来自JS背景,是Java的新手。
我基本上创建了一个:
User username = new User(Double startingMoney,
String fName, String lName, String userName, String passWord);
然后你会做以下事情:
username.setBalance(Double balanceAmt); or username.getBalance();
问题是,我不确定如何命名对象。我希望我的程序允许用户注册一个新的用户帐户,并使用他们拥有的任何用户名,作为对象的名称。
在JS中,您可以使用String作为对象变量名。
我甚至不确定这是否是创建新用户的正确方法。我不确定以后如何引用特定对象,为用户设置余额。
IE:我有两个用户......因此有两个用户对象......一个约翰和一个贝蒂。
如果当前登录了betty,我不确定我是如何调用betty.setBalance(20.00);
有任何建议或指示吗?感谢。
答案 0 :(得分:2)
应该制作一个类用户。可以制作通常的制定者和吸气剂。但要实例化你所要求的,你可以在实例化对象时给出参数中的参数。我已经为该类添加了2个构造函数作为示例。
public class User
{
private double startingMoney;
private String firstName;
private String lastName;
private String username;
private String password;
private static int count = 0; // number of objects in memory
// initialize user, add 1 to static count and
// output String indicating that constructor was called
public User( String first, String last )
{
firstName = first;
lastName = last;
count++; // increment static count of Users
System.out.printf( "User constructor: %s %s; count = %d\n",
firstName, lastName, count );
} // end User constructor
private double startingMoney;
private String firstName;
private String lastName;
private String username;
private String password;
//another constructer
public User( Double sMoney, String first, String last, String name, String pass)
{
startingMoney = sMoney
firstName = first;
lastName = last;
username = name;
password = pass;
count++; // increment static count of Users
System.out.printf( "User constructor: %s %s; count = %d\n",
firstName, lastName, count );
} // end User constructor
// subtract 1 from static count when garbage
// collector calls finalize to clean up object;
// confirm that finalize was called
protected void finalize()
{
count--; // decrement static count of users
System.out.printf( "User finalizer: %s %s; count = %d\n",
firstName, lastName, count );
} // end method finalize
// set first name
public String setFirstName(String firstName)
{
this.firstName = firstName;
} // end method setFirstName
// set last name
public String setLastName(String lastName)
{
this.lastName = lastName;
} // end method setLastName
// set user name
public String setUserName(String name)
{
this.username = name;
} // end method setUserName
// set password
public String setPassword(String password)
{
this.password = password;
} // end method setPassword
public String getStartingName()
{
return startingName;
} // end method getstartingName
// get username
public String getUserName()
{
return username;
} // end method getUserName
// get password
public String getPassword()
{
return password;
} // end method getPassword
// get first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// get last name
public String getLastName()
{
return lastName;
} // end method getLastName
// static method to get static count value
public static int getCount()
{
return count;
} // end method getCount
}
答案 1 :(得分:0)
首先创建一个类,然后为该类创建两个对象。
class User {
//Constructor
User(Double startingMoney, String fName, String lName, String userName, String passWord) {
this.startingMoney=startingMoney;
this.fName=fName;
this.lName=lName;
this.userName=userName;
this.passWord=passWord;
}
double startingMoney;
String fName;
String lName;
String userName;
String passWord;
}
User john = new User(..Values.); //pass arguments related to john
User betty= new User(..Values.); //pass arguments related to betty
john.getLName();
betty.setPassword("pass");
in java you cant create a runtime variable; better do this way
whenever user is arrived; use a Map
Map.add("userName", new User(...Values..)); //userName as key
then get that object like Map.get("userName")
答案 2 :(得分:0)
这样做的一种方法是使用Map。在哪里使用名称作为键,值将是User对象。
Map <String, User> users = new Hashmap<String, User>();
users.put("betty", new User(...));
users.get("betty").setBalance(20.00);