三种对象初始化代码,有何不同求解答


1.创建对象的模式1



public class LunchModelOne {
   private String s1;
   private String s2;
   private String s3;
   private String s4;
   private String s5;
   private String s6;
   
   public LunchModelOne(){
    this(null);
   }
   public LunchModelOne(String s1){
    this(s1,null);
   }
   public LunchModelOne(String s1,String s2){
    this(s1,s2,null);
   }
   public LunchModelOne(String s1,String s2,String s3){
    this(s1,s2,s3,null);
   }
   public LunchModelOne(String s1,String s2,String s3,String s4){
    this(s1,s2,s3,s4,null);
   }
   public LunchModelOne(String s1,String s2,String s3,String s4,String s5){
    this(s1,s2,s3,s4,s5,null);
   }
   public LunchModelOne(String s1,String s2,String s3,String s4,String s5,String s6){
    this.s1=s1;
    this.s2=s2;
    this.s3=s3;
    this.s4=s4;
    this.s5=s5;
    this.s6=s6;
   }
}

2.创建对象的模式2



public class LunchModelTwo {
private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
 
public LunchModelTwo(){}
public LunchModelTwo setS1(String s1){
    this.s1=s1;
    return this;
}
public LunchModelTwo setS2(String s2){
    this.s2=s2;
    return this;
}
public LunchModelTwo setS3(String s3){
    this.s3=s3;
    return this;
}
public LunchModelTwo setS4(String s4){
    this.s4=s4;
    return this;
 }
public LunchModelTwo setS5(String s5){
    this.s5=s5;
    return this;
}
public LunchModelTwo setS6(String s6){
    this.s6=s6;
    return this;
}
}

3.创建对象的模式3



public class LunchModelThree {
private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
 
public static class Builder{
private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
 
public Builder(){}
 
public Builder setS1(String s1){
    this.s1=s1;
    return this;
}
public Builder setS2(String s2){
    this.s2=s2;
    return this;
}
public Builder setS3(String s3){
    this.s3=s3;
    return this;
}
public Builder setS4(String s4){
    this.s4=s4;
    return this;
 }
public Builder setS5(String s5){
    this.s5=s5;
    return this;
}
public Builder setS6(String s6){
    this.s6=s6;
    return this;
}
public LunchModelThree create(){
return new LunchModelThree(this);
}
}
private LunchModelThree(Builder builder){
this.s1=builder.s1;
this.s2=builder.s2;
this.s3=builder.s3;
this.s4=builder.s4;
this.s5=builder.s5;
this.s6=builder.s6;
}
 public String toString(){
    return new StringBuffer().append("s1="+s1).append(";s2="+s2)
    .append(";s3="+s3).append(";s4="+s4).append(";s5="+s5).append(";s6="+s6).toString();
   }

测试:



public class LunchModelThreeTest {
public static void main(String[]args){
LunchModelThree.Builder builder = new LunchModelThree.Builder();  
    LunchModelThree lunch = builder.setS1("s1").setS2("s2").setS3("s3").create();  
    
    System.out.println(lunch.toString());  
}
}


谁能给个意见说一下以上三种模式的好坏?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值