博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学习:类的多属性问题。
阅读量:7222 次
发布时间:2019-06-29

本文共 3918 字,大约阅读时间需要 13 分钟。

场景一、

  已知一个类有三个必须的属性、有十个可有可无的属性,要求该类一旦初始化后不可以被修改

 

  

/** *  */package class_builder;/** * @author 9082046**@qq.com * */public class Person{   private final String lastName ;   private final String firstName;   private final String middleName;   private final String salutation;   private final String suffix;   private final String streetAddress;   private final String city;   private final String state;   private final boolean isFemale;   private final boolean isEmployed;   private final boolean isHomewOwner;    public Person(                  final String newLastName,                  final String newFirstName,                  final String newMiddleName,                  final String newSalutation,                  final String newSuffix,                  final String newStreetAddress,                  final String newCity,                  final String newState,                  final boolean newIsFemale,                  final boolean newIsEmployed,                  final boolean newIsHomeOwner)   {      this.lastName = newLastName;      this.firstName = newFirstName;      this.middleName = newMiddleName;      this.salutation = newSalutation;      this.suffix = newSuffix;      this.streetAddress = newStreetAddress;      this.city = newCity;      this.state = newState;      this.isFemale = newIsFemale;      this.isEmployed = newIsEmployed;      this.isHomewOwner = newIsHomeOwner;   }   public static class PersonBuilder   {      private String nestedLastName;      private String nestedFirstName;      private String nestedMiddleName;      private String nestedSalutation;      private String nestedSuffix;      private String nestedStreetAddress;      private String nestedCity;      private String nestedState;      private boolean nestedIsFemale;      private boolean nestedIsEmployed;      private boolean nestedIsHomeOwner;       public PersonBuilder(         final String newFirstName,         final String newCity,         final String newState)       {         this.nestedFirstName = newFirstName;         this.nestedCity = newCity;         this.nestedState = newState;      }       public PersonBuilder lastName(String newLastName)      {         this.nestedLastName = newLastName;         return this;      }       public PersonBuilder firstName(String newFirstName)      {         this.nestedFirstName = newFirstName;         return this;      }       public PersonBuilder middleName(String newMiddleName)      {         this.nestedMiddleName = newMiddleName;         return this;      }       public PersonBuilder salutation(String newSalutation)      {         this.nestedSalutation = newSalutation;         return this;      }       public PersonBuilder suffix(String newSuffix)      {         this.nestedSuffix = newSuffix;         return this;      }       public PersonBuilder streetAddress(String newStreetAddress)      {         this.nestedStreetAddress = newStreetAddress;         return this;      }       public PersonBuilder city(String newCity)      {         this.nestedCity = newCity;         return this;      }       public PersonBuilder state(String newState)      {         this.nestedState = newState;         return this;      }       public PersonBuilder isFemale(boolean newIsFemale)      {         this.nestedIsFemale = newIsFemale;         return this;      }       public PersonBuilder isEmployed(boolean newIsEmployed)      {         this.nestedIsEmployed = newIsEmployed;         return this;      }       public PersonBuilder isHomeOwner(boolean newIsHomeOwner)      {         this.nestedIsHomeOwner = newIsHomeOwner;         return this;      }       public Person createPerson()      {         return new Person(            nestedLastName, nestedFirstName, nestedMiddleName,            nestedSalutation, nestedSuffix,            nestedStreetAddress, nestedCity, nestedState,            nestedIsFemale, nestedIsEmployed, nestedIsHomeOwner);      }   }}

 

转载地址:http://yrzfm.baihongyu.com/

你可能感兴趣的文章
[case分享]Exchange 2010 登陆OWA查看邮件出现Rights managem operation failed
查看>>
linux dd 读取 写入磁盘速度
查看>>
dmidecode查看linux硬件信息
查看>>
linux监控对象及重要性
查看>>
walle-web自动化部署配置
查看>>
opencv轮廓提取、轮廓识别相关要点
查看>>
BOOST.ASIO源码剖析(一)
查看>>
过滤squidlog中各个链接的大小
查看>>
我的友情链接
查看>>
使用AnyChat如何实现任意两用户之间的音视频交互
查看>>
【个人小结】项目公共js的配置,解决不同页面多个配置修改的问题
查看>>
XAMP安装Apacher无法启动
查看>>
mongodb user
查看>>
ip地址子网划分
查看>>
Linux下快速搭建ntp时间同步服务器
查看>>
TouchEvent的传递过程学习笔记
查看>>
Android笔记--TCP Scoket(字符串收发)
查看>>
我的友情链接
查看>>
Hunt framework 2.0.0 发布,简单且高性能的 Web 服务框架
查看>>
数据库原理及应用(SQL Server 2016数据处理)【上海精品视频课程】
查看>>