클래스 a를 aX로 확장하려고합니다. 그래서 빌더를 확장합니다. 그러나 클래스 A의 객체를 만들 수있는 동안은 다음을 사용합니다.Builder 패턴을 사용하여 클래스 확장하기
aX에는 동일하게 적용되지 않습니다. 나는이 작업을 수행 할 때 :
aXBuilder fb = new aXBuilder();
aX aXtry = fb.withI(i).withS(s).withB(b).build();
를 내가 오류 (메소드 withB (부울) 유형 a.aBuilder에 대한 정의이다). 단순히 새로운 것을 추가하는 대신 aX에 대한 모든 내용을 다시 작성해야합니까? 나는 그것이 내 코드에 많은 중복을 초래할 것이기 때문에 그렇게하고 싶지 않다. 클래스 A와 도끼가 아래에서 언급과 같이
class a {
protected String s;
protected int i;
public void getdata() {
System.out.println(this.s);
System.out.println(this.i);
}
protected a(aBuilder fb) {
this.s = fb.bs;
this.i = fb.bi;
}
public static class aBuilder {
public aBuilder() {
}
protected String bs;
protected int bi;
public aBuilder withS(String s) {
this.bs = s;
return this;
}
public aBuilder withI(Integer i) {
this.bi = i;
return this;
}
public a build() {
return new a(this);
}
}
}
클래스 도끼는 방법이없는 aBuilder
하는 {
protected Boolean b;
public void getData()
{
System.out.println(this.s);
System.out.println(this.i);
System.out.println(this.b);
}
protected aX(aXBuilder axb) {
super(axb);
this.b = axb.bb;
}
public static class aXBuilder extends aBuilder {
protected Boolean bb;
public aXBuilder() {
}
public aXBuilder withB(Boolean b) {
this.bb = b;
return this;
};
public aX build() {
return new aX(this);
}
}
}
당신이는 그 질문을보고 있었나요? http://stackoverflow.com/questions/21086417/builder-pattern-and-inheritance and http://stackoverflow.com/questions/17164375/subclassing-a-java-builder-class? – John