以模拟计算器的加减乘除为例 计算器类
public class Calc {
private Integer value;
public Calc() {
this.value = 0;
}
public Calc add(Integer add) {
this.value += add;
return this;
}
public Calc sub(Integer sub) {
this.value -= sub;
return this;
}
public Calc mul(Integer mul) {
this.value *= mul;
return this;
}
public Calc div(Integer div) {
this.value /= div;
return this;
}
public Integer getValue() {
return this.value;
}
}
测试类
public class Main {
public static void main(String[] args) {
Calc calc = new Calc();
calc.add(15).sub(5).mul(10).div(25);
System.out.println(calc.getValue());
}
}
效果
D:\Java_JDK\JDK8u202\bin\java.exe "-javaagent:D:\JetBrainsDeveloperTools\IntelliJ IDEA 2021.2\lib\idea_rt.jar=3225:D:\JetBrainsDeveloperTools\IntelliJ IDEA 2021.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Java_JDK\JDK8u202\jre\lib\charsets.jar;D:\Java_JDK\JDK8u202\jre\lib\deploy.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\access-bridge-64.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\cldrdata.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\dnsns.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\jaccess.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\jfxrt.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\localedata.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\nashorn.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\sunec.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\sunjce_provider.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\sunmscapi.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\sunpkcs11.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\zipfs.jar;D:\Java_JDK\JDK8u202\jre\lib\javaws.jar;D:\Java_JDK\JDK8u202\jre\lib\jce.jar;D:\Java_JDK\JDK8u202\jre\lib\jfr.jar;D:\Java_JDK\JDK8u202\jre\lib\jfxswt.jar;D:\Java_JDK\JDK8u202\jre\lib\jsse.jar;D:\Java_JDK\JDK8u202\jre\lib\management-agent.jar;D:\Java_JDK\JDK8u202\jre\lib\plugin.jar;D:\Java_JDK\JDK8u202\jre\lib\resources.jar;D:\Java_JDK\JDK8u202\jre\lib\rt.jar;D:\My Projects\SpringMVC\ssm_demo\out\production\test" top.harmonytqs.Main
4
Process finished with exit code 0
Person类
@Accessors(chain = true)//@Accessors(chain = true)使用此注解开启链式编程
@Data
@NoArgsConstructor
public class Person {
private String name;
private Integer age;
private String gender;
}
测试类
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("张三").setAge(21).setGender("男");
System.out.println(person);
}
}
效果
D:\Java_JDK\JDK8u202\bin\java.exe "-javaagent:D:\JetBrainsDeveloperTools\IntelliJ IDEA 2021.2\lib\idea_rt.jar=10944:D:\JetBrainsDeveloperTools\IntelliJ IDEA 2021.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Java_JDK\JDK8u202\jre\lib\charsets.jar;D:\Java_JDK\JDK8u202\jre\lib\deploy.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\access-bridge-64.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\cldrdata.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\dnsns.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\jaccess.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\jfxrt.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\localedata.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\nashorn.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\sunec.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\sunjce_provider.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\sunmscapi.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\sunpkcs11.jar;D:\Java_JDK\JDK8u202\jre\lib\ext\zipfs.jar;D:\Java_JDK\JDK8u202\jre\lib\javaws.jar;D:\Java_JDK\JDK8u202\jre\lib\jce.jar;D:\Java_JDK\JDK8u202\jre\lib\jfr.jar;D:\Java_JDK\JDK8u202\jre\lib\jfxswt.jar;D:\Java_JDK\JDK8u202\jre\lib\jsse.jar;D:\Java_JDK\JDK8u202\jre\lib\management-agent.jar;D:\Java_JDK\JDK8u202\jre\lib\plugin.jar;D:\Java_JDK\JDK8u202\jre\lib\resources.jar;D:\Java_JDK\JDK8u202\jre\lib\rt.jar;D:\My Projects\SpringMVC\ssm_demo\out\production\test;D:\My Projects\SpringMVC\ssm_demo\test\src\top\harmonytqs\lombok-1.18.20.jar" top.harmonytqs.Main
Person(name=张三, age=21, gender=男)
Process finished with exit code 0
Q.E.D.