用Object类 判断账号密码是否相同
get,set,必须使用
程序代码:
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."); }