1 回答
TA贡献1810条经验 获得超5个赞
这非常简单 - 的方法签名期望为实际方法调用“即时”设置Immutable#builder类型参数,而不是绑定到类。T要适当地覆盖此方法,签名ConcreteClass将是
public <T> Builder<T> builder() {
这显然与您的构建器定义冲突
return new AbstractClass.AbstractBuilder<ConcreteClass>(this);
为了使这一切都可编译,你必须从类而不是方法调用者中推断出T,Immutable#builder即你终于有了
public interface Immutable<T> {
public interface Builder<T> {
public T build();
}
public Builder<T> builder();
}
并且所有继承类都相应地更改以将 a 传递T给它的前辈。
public interface Interface<T> extends Immutable<T> {
public interface BuilderInterface<T> extends Immutable.Builder<T> {
}
}
public abstract class AbstractClass<T> implements Interface<T> {
public static class AbstractBuilder<T> implements Interface.BuilderInterface<T> {
private final T object;
public AbstractBuilder(T object) {
this.object = object;
}
@Override
public T build() {
return this.object;
}
}
protected AbstractClass() {
super();
}
}
public class ConcreteClass extends AbstractClass<ConcreteClass> {
public ConcreteClass() {
}
@Override
public Builder<ConcreteClass> builder() {
return new AbstractClass.AbstractBuilder<ConcreteClass>(this);
}
}
添加回答
举报