温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

简单的builder构造器示列

发布时间:2020-07-22 05:41:54 来源:网络 阅读:466 作者:rgixb586 栏目:开发技术

Java代码  简单的builder构造器示列

  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. }  


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI