zoukankan      html  css  js  c++  java
  • 学习:类的多属性问题。

    场景一、

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

      

    /**
     * 
     */
    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);
          }
       }
    }
  • 相关阅读:
    用VSTS进行网站压力测试
    紧急求助!powerdesigner 12的问题!
    决定你是富人还是穷人的12法则(转)
    LINQ to SQL公共基类
    Web.config详解(转)
    向iframe中的页面传递参数
    [转]mysql 乱码问题解决终结
    [转]mysql多次调用存储过程的问题
    未能加载类型“System.Web.Mvc.ViewPage<String>”
    javascript之典型高阶函数
  • 原文地址:https://www.cnblogs.com/ribavnu/p/3600986.html
Copyright © 2011-2022 走看看