简单的builder构造器示列

Java代码   收藏代码
  1. /** 
  2.  * Created by baixiaobin  
  3.  */  
  4. public class User {  
  5.   
  6.     private final int id;  
  7.   
  8.     private final String name;  
  9.   
  10.     private final String sex;  
  11.   
  12.     private final String des;  
  13.   
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.   
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.   
  22.     public String getSex() {  
  23.         return sex;  
  24.     }  
  25.   
  26.     public String getDes() {  
  27.         return des;  
  28.     }  
  29.   
  30.     public static class Builder {  
  31.   
  32.         private final int id;  
  33.         private final String name;  
  34.   
  35.         private String sex;  
  36.   
  37.         private String des;  
  38.   
  39.         public User build() {  
  40.             return new User(this);  
  41.         } 下载  
  42.   
  43.         /** 
  44.          *  id   主键id 
  45.          * name 名称 
  46.          */  
  47.         public Builder(int id, String name) {  
  48.             this.id = id;  
  49.             this.name = name;  
  50.         }  
  51.   
  52.         public Builder sex(String sex) {  
  53.             this.sex = sex;  
  54.             return this;  
  55.         }  
  56.   
  57.         public Builder des(String des) {  
  58.             this.des = des;  
  59.             return this;  
  60.         }  
  61.   
  62.     }  
  63.   
  64.     private User(Builder builder) {  
  65.         this.id = builder.id;  
  66.         this.name = builder.name;  
  67.         this.des = builder.des;  
  68.         this.sex = builder.sex;  
  69.     }  
  70.   
  71. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值