我让一些片段扩展了 topFragment 类。但它有可变参数 - 自定义侦听器、一些模型等。
public abstract class TopFragment extend Fragment {
public interface OnCustomListener {
void onCustomListener();
}
protected OnCustomListener onCustomListener;
protected int importantValue;
protected String importantString;
protected abstract void doSomething();
public static class Builder() {
protected OnCustomListener onCustomListener;
protected int importantValue;
protected String importantString;
public Builder(OnCustomListener onCustomListener) {
this.onCustomListener = onCustomListener;
}
public Builder setValue(int value) {
this.importantValue = value;
return this;
}
public Builder setString(String value) {
this.importantString = value;
return this;
}
}
}
这是第二个片段类
public class SecondFragment extend TopFragment {
@Override
protected void doSomething() {
// do something.
}
}
public class ThirdFragment extend TopFragment {
@Override
protected void doSomething() {
// do something.
}
}
TopFragment fragment = new SecondFragment();
有用。那么如何用builder创建继承片段类的实例呢?