String sexStr = acc.getSex() == 30007 ? "先生" : "女士";
时间: 2024-05-21 09:13:34 浏览: 6
这段代码的作用是根据用户的性别(acc.getSex())判断是先生还是女士,并将结果保存在 sexStr 变量中。其中,30007 可能是一个特定的值,用于表示用户的性别(例如,约定 30007 表示男性,其他值表示女性)。如果 acc.getSex() 返回 30007,则 sexStr 被赋值为 "先生",否则 sexStr 被赋值为 "女士"。
相关问题
@Override public boolean equals(Object that) { if (this == that) { return true; } if (that == null) { return false; } if (getClass() != that.getClass()) { return false; } ApartmentAdmin other = (ApartmentAdmin) that; return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) && (this.getUserName() == null ? other.getUserName() == null : this.getUserName().equals(other.getUserName())) && (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword())) && (this.getRoleId() == null ? other.getRoleId() == null : this.getRoleId().equals(other.getRoleId())) && (this.getHeadimg() == null ? other.getHeadimg() == null : this.getHeadimg().equals(other.getHeadimg())) && (this.getPhone() == null ? other.getPhone() == null : this.getPhone().equals(other.getPhone())) && (this.getSex() == null ? other.getSex() == null : this.getSex().equals(other.getSex())) && (this.getBuildingNo() == null ? other.getBuildingNo() == null : this.getBuildingNo().equals(other.getBuildingNo())) && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) && (this.getIsLock() == null ? other.getIsLock() == null : this.getIsLock().equals(other.getIsLock())); }请详细解释每一行代码
这是一个Java类中的重写 equals 方法,用于比较两个对象是否相等。具体解释如下:
1. 第一行 @Override 注解表示该方法是重写了父类的 equals 方法。
2. 第二行开始是方法的定义,public 表示该方法是公共的,可以被其他类访问和调用;boolean 表示该方法返回一个布尔类型的值,即判断两个对象是否相等,返回true表示相等,返回false表示不相等。
3. 参数 Object that 表示传入该方法的对象,Object 是 Java 中所有类的父类,因此该方法可以接收任何类型的对象作为参数。
4. 第四行 if (this == that) 判断传入的对象是否与当前对象相同,如果是,则直接返回 true,表示两个对象相等。
5. 第五行 if (that == null) 判断传入的对象是否为 null,如果是,则直接返回 false,表示两个对象不相等。
6. 第六行 if (getClass() != that.getClass()) 判断传入的对象是否属于当前对象所属的类,如果不是,则直接返回 false,表示两个对象不相等。
7. 第七行到第十四行是比较两个对象中的属性是否相等,如果相等,则返回 true,否则返回 false。其中使用了三元运算符,如果属性为 null,则需要使用 equals 方法进行判断,而不能直接使用 == 进行判断。
8. 最后,该方法判断的属性包括:id、用户名、密码、角色id、头像、电话、性别、楼号、创建时间、是否锁定。
public void userReg(HttpServletRequest req,HttpServletResponse res) { String id=String.valueOf(new Date().getTime()); String loginname=req.getParameter("loginname"); String loginpw=req.getParameter("loginpw"); String name=req.getParameter("name"); String sex=req.getParameter("sex"); String age=req.getParameter("age"); String address=req.getParameter("address"); String tel=req.getParameter("tel"); String email=req.getParameter("email"); String qq=req.getParameter("qq"); String del="no"; String sql="insert into t_user values(?,?,?,?,?,?,?,?,?,?,?)"; Object[] params={id,loginname,loginpw,name,sex,age,address,tel,email,qq,del}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); String targetURL = "/common/add_success.jsp"; dispatch(targetURL, req, res); }修改为封装对象形式
public void userReg(HttpServletRequest req, HttpServletResponse res) {
// 创建 User 对象并设置属性值
User user = new User();
user.setId(String.valueOf(new Date().getTime()));
user.setLoginname(req.getParameter("loginname"));
user.setLoginpw(req.getParameter("loginpw"));
user.setName(req.getParameter("name"));
user.setSex(req.getParameter("sex"));
user.setAge(req.getParameter("age"));
user.setAddress(req.getParameter("address"));
user.setTel(req.getParameter("tel"));
user.setEmail(req.getParameter("email"));
user.setQq(req.getParameter("qq"));
user.setDel("no");
// 将 User 对象插入数据库
String sql = "insert into t_user values(?,?,?,?,?,?,?,?,?,?,?)";
Object[] params = {user.getId(), user.getLoginname(), user.getLoginpw(), user.getName(), user.getSex(), user.getAge(), user.getAddress(), user.getTel(), user.getEmail(), user.getQq(), user.getDel()};
DB mydb = new DB();
mydb.doPstm(sql, params);
mydb.closed();
String targetURL = "/common/add_success.jsp";
dispatch(targetURL, req, res);
}