非常简单的问题,但运气很少。
我的问题是,当我尝试在类getUserID
中调用类getPassword
的get User
和Login
时,我得到null。
我看过其他与我有类似问题的帖子。虽然我发现很少有帖子可以指导我,但是一个常见的问题是海报没有初始化类对象,但我已经完成了。
我在这里需要特定的代码,因为我已经尝试了很多东西,但无法让它发挥作用。我希望我提供足够的信息来帮助你。
public class User
{
private String userID;
private String password;
private Employee employee;
private String authorityLevel;
/**
* Constructor for User class - Initialise a fixed password and employee object.
*/
public User()
{
employee = new Employee();
password = "password";
}
/**
* Create a user ID and print the user the details of their user account.
*/
public void createUser(Employee employee)
{
// Combine staff ID with authority key to make the user ID.
userID = employee.getID() + "" + employee.getAuthorityLevel();
// Check that there is a staff ID to create the user ID.
// It also ensures that an employee profile has been created before an attempt
// to make a user account.
if(employee.getID() == null){
System.out.println("There are no Employee details to make a User with.");
System.out.println("Please enter the Employee details before you make a user");
}
else{
System.out.println("Your user ID is: "+userID);
System.out.println("Your user password is: "+password);
}
}
/**
* @return The user ID.
*/
public String getUserID()
{
return userID;
}
/**
* @return The password.
*/
public String getPassword()
{
return password;
}
}
我想访问User类中的userID和password getter方法,以便在Login类中使用。
public class Login
{
private User user;
private boolean accessGranted;
private String userID;
private String password;
private boolean loggedIn;
private boolean loggedOut;
/**
* Constructor for the Login class - initialise a user object.
*/
public Login()
{
user = new User();
}
/**
* Attempt to start a login session.
*/
public void login(String userID,String password)
{
// Check that credentials entered are correct for the account the user wishes to log in to.
if((password == user.getPassword()) && (userID == user.getUserID())){
accessGranted = true;
if((accessGranted == true) && (userID.contains("H"))){
System.out.println("Your login session has started.");
System.out.println("You are now viewing Yuconz System as HR staff.");
}
if((accessGranted == true) && (userID.contains("D"))){
System.out.println("Your login session has started.");
System.out.println("You are now viewing Yuconz System as Director staff.");
}
if((accessGranted == true) && (userID.contains("E"))){
System.out.println("Your login session has started.");
System.out.println("You are now viewing Yuconz System as Employee staff.");
}
loggedIn = true;
}
else{
System.out.println("ACCESS DENIED BRUTHA!");
}
}
一个类创建用户帐户,其中唯一的详细信息是userID和密码。另一个类是用户可以使用该帐户的详细信息登录的地方。在Login
login
方法中,我检查输入的ID和密码是否与他们尝试访问的帐户的ID和密码相匹配。
答案 0 :(得分:1)
您似乎永远不会在User实例上调用public void createUser(Employee employee)
。
SO
private String userID;
从未初始化...
试试这个:
public class User{
private String userID;
private String password;
private Employee employee;
private String authorityLevel;
public User(Employee employee){
this.employee = employee;
password = "password";
createUser();
}
private void createUser(){
userID = employee.getID() + "" + employee.getAuthorityLevel();
if(userID == null){
System.out.println("There are no Employee details to make a User with.");
System.out.println("Please enter the Employee details before you make a user");
}else{
System.out.println("Your user ID is: "+userID);
System.out.println("Your user password is: "+password);
}
}
public String getUserID(){
return userID;
}
public String getPassword(){
return password;
}
}
如果您需要Employee实例来创建用户,那么将Employee传递给User的构造函数是最好的解决方案。
如果您的登录信息中没有Employee
的任何实例,只有id
和password
,那么您可以更改用户的构造函数::
public class User{
private String userID;
private String password;
private String authorityLevel;
public User(String userID, String password){
this.userID = userID;
this.password = password;
checkUser();
}
private void checkUser(){
if(userID == null){
System.out.println("There are no Employee details to make a User with.");
System.out.println("Please enter the Employee details before you make a user");
}else{
System.out.println("Your user ID is: " + userID);
System.out.println("Your user password is: " + password);
}
}
//...
}
您可以在User
Login
public class Login
private User user;
public Login(){
}
public void login(String userID,String password){
user = new User(userID, password);
//...
}
答案 1 :(得分:0)
字符串userID
和password
属于Employee
对象。您没有在User
课程中初始化这些值,这就是null
的原因。
你可以这样做
public String getUerID() {
return employee.getUserID();
如果您希望它们按照您在类中声明它们的方式属于User
类,则需要更改User
构造函数。 e.g。
public User(String id, String pwd) {
this.userID = id;
this.password = pwd;
}
然后你需要弄清楚你是否还想要Employee对象。
你应该重新考虑一些设计决定