子类继承父类时,在写代码时可能会有读取不到父类方法的时候:
需要用到泛型强转。如下:
public class test {
public static void main(String[] args) {
new BuilderTest()
.duration(100)//
.delay(100)//未解决之前两个方法中的其中一个无法读取到
.build();
}
}
public abstract class Builder {
private long delay ;
public long getDelay(){
return delay;
}
public T delay(long delay){
this.delay = delay;
return (T)this;
}
public abstract Event build();
}
public class BuilderTest extends Builder {
public int duration;
@Override
public Event build() {
return new PeriodEvent();
}
public BuilderTest duration(int duration){
this.duration = duration;
return this;
}
}