三种对象初始化代码,有何不同求解答
来源:互联网 发布:知乎的缺点 编辑:程序博客网 时间:2024/05/16 06:27
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());
}
}
谁能给个意见说一下以上三种模式的好坏?
- 三种对象初始化代码,有何不同求解答
- 视图中的OnCreate和OnInitialUpDate进行初始化有何不同?
- 视图中的OnCreate和OnInitialUpDate进行初始化有何不同?
- 视图中的OnCreate和OnInitialUpDate进行初始化有何不同?
- Hadoop和OpenStack对象存储有何不同?
- Hadoop分布式文件系统和OpenStack对象存储有何不同?
- Hadoop分布式文件系统和OpenStack对象存储有何不同
- Hadoop分布式文件系统和OpenStack对象存储有何不同?
- Hadoop分布式文件系统和对象存储有何不同?
- HDFS文件系统和OpenStack swift对象存储有何不同
- Hadoop分布式文件系统和OpenStack对象存储有何不同
- 视图中的OnCreate和OnInitialUpDate进行初始化有何不同[转]
- 在CView类的构造函数,OnCreate,或OnInitialUpDate中进行初始化有何不同?
- 初学vc8-视图中的OnCreate和OnInitialUpDate进行初始化有何不同?
- #java百问#2构造:定义是初始化String域,与构造器初始化String域,有何不同?
- JavaBean与EJB有何不同
- 管理与领导有何不同
- 管理与领导有何不同
- 58 最少步数
- POJ 1988 Cube Stacking(并查集)
- http://travel.tianya.cn/travelPlan/showPlan?planId=958409
- Spring接口ApplicationContextAware介绍及使用
- CF#254 (Div. 2) B.
- 三种对象初始化代码,有何不同求解答
- 派生类到基类转换的可访问性——新解
- 60 谁获得了最高奖学金
- HDU4870:Rating(DP)
- mysql数据库迁移
- Code Jam - Store Credit for Python
- VPN寻址及路由
- struts2 全局拦截器,显示请求方法和参数
- 62 笨小熊