集合的初始尝试使用之登录&注册小程序-----没有重写equles方法的后果
2016-10-11 22:19
393 查看
学完集合之后,写一下网站登录注册用的小程序练习;
任务:通过控制台,写一个登录注册的交互程序
源码目录如下:
src→com.sunwoda.logreginterf→LogRegInterf.java如下:
src→com.sunwoda.logregimpl→LogRegImpl.java如下:
src→com.sunwoda.user→User.java如下:
src→com.sunwoda.welcome→Welcome.java如下:
这样写看起来没有问题,运行起来也没有什么错误,但是就是不按逻辑来走。。。哪里不按逻辑走呢?
在运行时,首先输入2,进行注册,注册后输入1,开始登录,,可是怎么登就是登不上,问题出在哪儿呢?找来找去,只有这里有问题了:
怎么验证,程序就是不进
里面,判断输入数据,是否能个集合内元素匹配相等,结果看到的一直显示登录false。
问题就在这里,下面来看看contains()方法内部:
indexOf()方法内部:
这里面出现了equals()方法,并且是o的equals()方法,o是什么呢?
顺着看上去,o就是
中的传入user对象,那么,将登录输入数据和集合匹配的核心就在于通过equles()方法进行判断是否相同,,,再回头看看,上面写的User.java中并没有重写equles()方法,那么调用的就是默认的Object类的equles方法:
“==”只有在两个标识符指向同一个对象时才为true,显然在这里肯定是false,这就是输出总是为登录失败的原因所在了。
怎么解决呢?
答:重写User.java的equles()方法;
进入User.java,按下快捷键Alt+Shift+s,继续按h,即可自动重写equles方法,重写如下:
看下这个方法在什么情况下才会输出true,就会明白为什么要重写,
对于User对象来说,
要么传入User对象和此User对象同属一个对象,
要么传入User对象中的内容(内容也是就会account和password变量),和此User对象中的内容对应相等。
显然,我们在此小程序main方法中最终是通过后者来对比匹配相等,达到登录成功的目的。
任务:通过控制台,写一个登录注册的交互程序
源码目录如下:
src→com.sunwoda.logreginterf→LogRegInterf.java如下:
package com.sunwoda.logreginterf; import com.sunwoda.user.User; public interface LogRegInterf { public abstract boolean login(User user); public abstract boolean register(User user); }
src→com.sunwoda.logregimpl→LogRegImpl.java如下:
package com.sunwoda.logregimpl; import java.util.ArrayList; import java.util.Iterator; import com.sunwoda.logreginterf.LogRegInterf; import com.sunwoda.user.User; public class LogRegImpl implements LogRegInterf { ArrayList<User> ul = new ArrayList<User>(); @Override public boolean login(User user) { if(ul.contains(user)){ return true; } else{ System.out.println("用户名不存在!"); return false; } } @Override public boolean register(User user) { Iterator it = ul.iterator(); while(it.hasNext()){ User temp = (User)it.next(); if(user.getAccount().equals(temp.getAccount())){ System.out.println("用户名已存在!"); return false; } } ul.add(user); return true; } }
src→com.sunwoda.user→User.java如下:
package com.sunwoda.user; public class User { private String account; private String password; public User() { super(); } public User(String account, String password) { super(); this.account = account; this.password = password; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [account=" + account + ", password=" + password + "]"; } }
src→com.sunwoda.welcome→Welcome.java如下:
package com.sunwoda.welcome; import java.util.Scanner; import com.sunwoda.logregimpl.LogRegImpl; import com.sunwoda.user.User; public class Welcome { public static void main(String[] args) { LogRegImpl lr = new LogRegImpl(); String account; String password; boolean result; for(;;){ System.out.println("请输入序号,选择操作:\n1,登录\n2,注册\n3,退出"); int key = new Scanner(System.in).nextInt(); switch(key){ case 1: System.out.println("账号:"); account = new Scanner(System.in).nextLine(); System.out.println("密码:"); password = new Scanner(System.in).nextLine(); result = lr.login(new User(account,password)); System.out.println(result); break; case 2: System.out.println("账号:"); account = new Scanner(System.in).nextLine(); System.out.println("密码:"); password = new Scanner(System.in).nextLine(); result = lr.register(new User(account,password)); System.out.println(result); break; case 3: System.exit(0); break; default: break; } } } }
这样写看起来没有问题,运行起来也没有什么错误,但是就是不按逻辑来走。。。哪里不按逻辑走呢?
在运行时,首先输入2,进行注册,注册后输入1,开始登录,,可是怎么登就是登不上,问题出在哪儿呢?找来找去,只有这里有问题了:
public boolean login(User user) { if(ul.contains(user)){ return true; } else{ System.out.println("用户名不存在!"); return false; } }
怎么验证,程序就是不进
if(ul.contains(user)){ return true; }
里面,判断输入数据,是否能个集合内元素匹配相等,结果看到的一直显示登录false。
问题就在这里,下面来看看contains()方法内部:
public boolean contains(Object o) { return indexOf(o) >= 0; }
indexOf()方法内部:
public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; }
这里面出现了equals()方法,并且是o的equals()方法,o是什么呢?
顺着看上去,o就是
if(ul.contains(user)){ return true; }
中的传入user对象,那么,将登录输入数据和集合匹配的核心就在于通过equles()方法进行判断是否相同,,,再回头看看,上面写的User.java中并没有重写equles()方法,那么调用的就是默认的Object类的equles方法:
public boolean equals(Object obj) { return (this == obj); }
“==”只有在两个标识符指向同一个对象时才为true,显然在这里肯定是false,这就是输出总是为登录失败的原因所在了。
怎么解决呢?
答:重写User.java的equles()方法;
进入User.java,按下快捷键Alt+Shift+s,继续按h,即可自动重写equles方法,重写如下:
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (account == null) { if (other.account != null) return false; } else if (!account.equals(other.account)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; return true; }
看下这个方法在什么情况下才会输出true,就会明白为什么要重写,
对于User对象来说,
要么传入User对象和此User对象同属一个对象,
要么传入User对象中的内容(内容也是就会account和password变量),和此User对象中的内容对应相等。
显然,我们在此小程序main方法中最终是通过后者来对比匹配相等,达到登录成功的目的。
相关文章推荐
- "在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误"的解决方法
- 使用AjaxPro出现错误(没有找到适合的方法来重写)
- 关于asp.net网站发布后,使用登录控件和注册控件时出现“数据库只读,无法进行数据更新”的解决方法
- “屏幕保护程序”没有出现“在恢复时使用密码保护”的解决方法
- “在应用程序级别之外使用注册为 allowDefinition=’MachineToApplication’ 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误”的解决方法。
- C:\Windows\system32\MSVCR100.dll 没有被指定在 Windows 上运行,或者它包含错误。请尝试使用原始安装媒体重新安装程序,或联系您的系统管理员或软件供应商以获取支持。【解决办法】
- “在应用程序级别之外使用注册为 allowDefinition=’MachineToApplication’ 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误”的解决方法
- 没有为扩展名“.html”注册的生成提供程序。.net urlwrite 重写 伪静态 IIs 配置
- “在应用程序级别之外使用注册为 allowDefinition=’MachineToApplication’ 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误”的解决方法。
- VS使用进程调试出现“没有正确安装调试器。请运行安装程序安装或修复调试器”的错误,解决方法。
- 使用VS2003遇到“无法显示进程。没有正确安装调试器。请运行安装程序安装或修复调试器。”的解决方法
- 在应用程序级别之外使用注册为 allowDefinition=’MachineToApplication’ 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误”的解决方法
- win 7 删除了用户文件夹后出现的&quot;你已使用临时配置文件登录&quot;提示的解决方法
- 通用的DAO查询,常见方法集合,没有使用Spring
- [置顶] 通用的DAO查询,常见方法集合,没有使用Spring
- “在应用程序级别之外使用注册为 allowDefinition=’MachineToApplication’ 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误”的解决方法
- 没有为扩展名“.html”注册的生成提供程序。.net urlwrite 重写 伪静态 IIs 配置
- 没有为扩展名“.html”注册的生成提供程序。.net urlwrite 重写 伪静态 IIs 配置
- 使用java写一个没有main方法的“Hello World”程序(摘自java核心技术.卷I)
- 使用Membership,您的登录尝试不成功。请重试"的解决方法