Java链式写法

时间:2022-05-06 20:18:10
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11613067.html

Java 链式写法:详细看代码

package chain;

/**
* TODO
* 解析器
*/
public class Parser { private String id;
private String mode;
private String concatIdMode; public static Parser parse(){
return new Parser();
} public Parser setParserId(String id){
this.id = id;
return this;
} public Parser setParserMode(String mode){
this.mode = mode;
return this;
} public Parser concat(){
this.concatIdMode = "id: " + this.id + " " + "模式 : " + this.mode;
return this;
} public String print(){
System.out.println("解析器的id和模式为: " + this.concatIdMode); return this.concatIdMode;
}
}
package chain;

/**
* TODO
* 链式
*/
public class Chains { public static void main(String[] args) {
String concat = Parser.parse()
.setParserId("12")
.setParserMode("dev")
.concat()
.print();
System.out.println(concat);
}
}

打印结果:

Java链式写法