A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© shaoshuai 中级黑马   /  2015-2-5 11:32  /  1105 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 shaoshuai 于 2015-2-5 11:33 编辑

话不多少,直接上代码:
  1. public class User {
  2.         
  3.         private long id;
  4.         private String username;
  5.         private String password;
  6.         private String email;
  7.         private String address;
  8.         
  9.         public User(){
  10.                
  11.         }
  12.         
  13.         public User(UserBuilder builder) {
  14.                 this.id = builder.id;
  15.                 this.username = builder.username;
  16.                 this.password = builder.password;
  17.                 this.email = builder.email;
  18.                 this.address = builder.address;
  19.         }

  20.         public long getId() {
  21.                 return id;
  22.         }
  23.         public String getUsername() {
  24.                 return username;
  25.         }
  26.         public String getPassword() {
  27.                 return password;
  28.         }
  29.         public String getEmail() {
  30.                 return email;
  31.         }
  32.         public String getAddress() {
  33.                 return address;
  34.         }
  35.         
  36.         @Override
  37.         public int hashCode() {
  38.                 final int prime = 31;
  39.                 int result = 1;
  40.                 result = prime * result + (int) (id ^ (id >>> 32));
  41.                 return result;
  42.         }

  43.         @Override
  44.         public boolean equals(Object obj) {
  45.                 if (this == obj)
  46.                         return true;
  47.                 if (obj == null)
  48.                         return false;
  49.                 if (getClass() != obj.getClass())
  50.                         return false;
  51.                 User other = (User) obj;
  52.                 if (id != other.id)
  53.                         return false;
  54.                 return true;
  55.         }

  56.         static class UserBuilder {
  57.                 private long id;
  58.                 private String username;
  59.                 private String password;
  60.                 private String email;
  61.                 private String address;
  62.                
  63.                 public UserBuilder(long id, String username, String password) {
  64.                         this.id = id;
  65.                         this.username = username;
  66.                         this.password = password;
  67.                 }
  68.                
  69.                 public UserBuilder email(String email) {
  70.                         this.email = email;
  71.                         return this;
  72.                 }
  73.                
  74.                 public UserBuilder address(String address) {
  75.                         this.address = address;
  76.                         return this;
  77.                 }
  78.                
  79.                 public User build() {
  80.                         return new User(this);
  81.                 }
  82.         }

  83.         
  84. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马