class A
{
private A(){}
public static A.Builder Builder()
{
/**
* ERROR:
* No enclosing instance of type A is accessible.
* Must qualify the allocation with an enclosing instance of type A
* (e.g. x.new A() where x is an instance of A).
*/
return new A.Builder();
// Error too
//return new Builder();
}
public class Builder
{
private Builder()
{}
}
}
問:如何實例構建器,但不要更改靜態生成器和嵌套類名?
編輯
如果類是靜態的,如何節約每一個建設者的日期?如何鏈接構建過程?
public static class Builder
{
private Builder()
{}
public Builder add(int a)
{
return this;// how to chain the build process ?
}
public Builder add(float a);
public List<Double> Build();
}
好的,我應該首先谷歌Java生成器模式。 Here就是一個例子。
請不要編輯以提出一個無關的新問題。問一個新的。 – aalku