【发布时间】:2014-03-05 11:17:45
【问题描述】:
代码优先。
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()
{}
}
}
问:如何实例化builder而不改变静态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();
}
好的,我应该先google java builder模式。Here就是一个例子。
【问题讨论】:
-
请不要编辑以提出不相关的新问题。问一个新的。
标签: java class instance builder