构建器模式完全可以代替javabean模式和重叠构造器模式,只是目前用的人非常少而已 public class Outer { private final String name; private final int age; public static class Inner{ private final String name1; pri
构建器模式完全可以代替javabean模式和重叠构造器模式,只是目前用的人非常少而已
public class Outer {private final String name;
private final int age;
public static class Inner{
private final String name1;
private int age1=0;
public String getName1() {
return name1;
}
public int getAge1() {
return age1;
}
public Inner(String name){
this.name1=name;
}
public Inner age(int val){
this.age1=val;
return this;
}
public Outer build(){
return new Outer(this);
}
}
private Outer(Inner neibu){
name=neibu.name1;
age=neibu.age1;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
//----------------------------
public static void main(String[] args) {
Outer outer=new Outer.Inner("doublift").age(18).build();
System.out.println(outer.getName());
System.out.println(outer.getAge());
}