用Object类 判断账号密码是否相同(Java) get,set,必须使用 提供代码
时间: 2024-05-06 07:19:19 浏览: 6
以下是判断账号密码是否相同的Java代码,使用了Object类:
```
public class Account {
private Object username;
private Object password;
public Object getUsername() {
return username;
}
public void setUsername(Object username) {
this.username = username;
}
public Object getPassword() {
return password;
}
public void setPassword(Object password) {
this.password = password;
}
public boolean isSame(Account other) {
return this.username.equals(other.getUsername()) && this.password.equals(other.getPassword());
}
}
```
使用该类时,可先设置账号和密码:
```
Account myAccount = new Account();
myAccount.setUsername("myUserName");
myAccount.setPassword("myPassword");
```
然后,再创建另一个Account对象并与myAccount比较是否相同:
```
Account otherAccount = new Account();
otherAccount.setUsername("myUserName");
otherAccount.setPassword("myPassword");
if (myAccount.isSame(otherAccount)) {
System.out.println("The username and password are the same.");
} else {
System.out.println("The username and password are different.");
}
```