流利语法,fluent syntax,可以实现方法级联,书写起来比较优雅。
netty中也有类似写法。
public abstract class AbstractBootstrap <B extends AbstractBootstrap<B,C>,C extends Channel>
In this signature, the subclass B is a type parameter to the superclass, so that a refer- ence to the runtime instance can be returned to support method chaining (so-called fluent syntax).
假设有父类A
public class A <B extends A>{
String name;
int age;
public String getName() {
return name;
}
public B setName(String name) {
this.name = name;
return (B) this;
}
public int getAge() {
return age;
}
public B setAge(int age) {
this.age = age;
return (B) this;
}
}
假设有子类B
public class B extends A{
public static void main(String[] args) {
B b = new B();
b.setAge(111).
setName("222");//方法级联
}
}
网友评论