嗨,抱歉,如果这是一个愚蠢的问题,我只是被困在它。我试图创建新用户驱动程序类以及检查密码是否有效和停用账户。任何帮助都将非常感激。此外,在正确的方向点或一个类似的代码的例子将是伟大的。我不只是在寻找答案,我还想知道为什么……
提前感谢如果你会读这一切!(:
这是我创建变量的类
<<p> 更新代码/strong>好了,所以我回去做了一些改变(更新了上面的代码),用户名,密码和活动状态显示为我想要的。如何使帐户默认激活?我也不知道如何编写deactivateAccount方法,使其更改为false
private String username;
private String password;
private boolean active;
public UserAccount() {
active = true;
}
public UserAccount(String username, String password) {
this.username = username;
this.password = password;
}
public boolean checkPassword(String password) {
if (this.password.equals(password)) {
return true;
}
else {
return false;
}
}
public void deactivateAccount(String username){
username = this.username;
active = false;
}
@Override
public String toString() {
return username + password + active;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof UserAccount))
return false;
UserAccount other = (UserAccount) obj;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
这是驱动类
类UserDriver {
public static void main(String[] args) {
UserAccount user1 = new UserAccount(" Rachel ", "Birthday ");
UserAccount user2 = new UserAccount(" Brandi ", "Purple ");
System.out.println("User 1: " + user1);
System.out.println("User 2: " + user2);
if (user1.equals(user2)) {
System.out.println("The users are equilvalent");
}
else {
System.out.println("The users are not equivilent");
}
}
}
传递的不是字符串,而是未声明的变量。把
UserAccount user1 = new UserAccount(Rachel, Birthday);
UserAccount user2 = new UserAccount(Brandi, Purple);
// What are the Rachel, Birthday, Brandi and Purple?
UserAccount user1 = new UserAccount("Rachel", "Birthday"); // ah now they're Strings!
UserAccount user2 = new UserAccount("Brandi", "Purple");
将来,如果您需要询问有关编译错误或抛出异常的问题,请发布完整的消息供我们评估。
if (password.equals(password))
将传递的参数与自身进行比较。
尝试if (password.equals(this.password))
这些会编译第一件事吗
UserAccount user1 = new UserAccount(Rachel, Birthday);
UserAccount user2 = new UserAccount(Brandi, Purple);
作为public UserAccount(String username, String password)
接受String参数
System.out.println(user1.toString());
System.out.println(user2.toString());
它只会输出对象的哈希值
然后是user1.checkPassword(password);
密码是什么现在我看不到任何声明或初始化假设你已经初始化了然后调用到
public boolean checkPassword(String password)
它又很混乱,因为你的局部变量的名字与实例变量冲突,为了避免它,只需在实例变量上使用这个关键字像
if (this.password.equals(password)){//do this}