Java链式编程

2022-10-25 19:26:24 浏览数 (2)

1. 链式编程原理:调用方法返回当前对象引用。

以模拟计算器的加减乘除为例 计算器类

代码语言:javascript复制
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;
    }
}

测试类

代码语言:javascript复制
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());
    }
}

效果

代码语言:javascript复制
D:Java_JDKJDK8u202binjava.exe "-javaagent:D:JetBrainsDeveloperToolsIntelliJ IDEA 2021.2libidea_rt.jar=3225:D:JetBrainsDeveloperToolsIntelliJ IDEA 2021.2bin" -Dfile.encoding=UTF-8 -classpath "D:Java_JDKJDK8u202jrelibcharsets.jar;D:Java_JDKJDK8u202jrelibdeploy.jar;D:Java_JDKJDK8u202jrelibextaccess-bridge-64.jar;D:Java_JDKJDK8u202jrelibextcldrdata.jar;D:Java_JDKJDK8u202jrelibextdnsns.jar;D:Java_JDKJDK8u202jrelibextjaccess.jar;D:Java_JDKJDK8u202jrelibextjfxrt.jar;D:Java_JDKJDK8u202jrelibextlocaledata.jar;D:Java_JDKJDK8u202jrelibextnashorn.jar;D:Java_JDKJDK8u202jrelibextsunec.jar;D:Java_JDKJDK8u202jrelibextsunjce_provider.jar;D:Java_JDKJDK8u202jrelibextsunmscapi.jar;D:Java_JDKJDK8u202jrelibextsunpkcs11.jar;D:Java_JDKJDK8u202jrelibextzipfs.jar;D:Java_JDKJDK8u202jrelibjavaws.jar;D:Java_JDKJDK8u202jrelibjce.jar;D:Java_JDKJDK8u202jrelibjfr.jar;D:Java_JDKJDK8u202jrelibjfxswt.jar;D:Java_JDKJDK8u202jrelibjsse.jar;D:Java_JDKJDK8u202jrelibmanagement-agent.jar;D:Java_JDKJDK8u202jrelibplugin.jar;D:Java_JDKJDK8u202jrelibresources.jar;D:Java_JDKJDK8u202jrelibrt.jar;D:My ProjectsSpringMVCssm_demooutproductiontest" top.harmonytqs.Main
4

Process finished with exit code 0

2. Lombok中可使用setter链式编程为属性赋值

Person类

代码语言:javascript复制
@Accessors(chain = true)//@Accessors(chain = true)使用此注解开启链式编程
@Data
@NoArgsConstructor
public class Person {
    private String name;
    private Integer age;
    private String gender;
}

测试类

代码语言:javascript复制
public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("张三").setAge(21).setGender("男");
        System.out.println(person);
    }
}

效果

代码语言:javascript复制
D:Java_JDKJDK8u202binjava.exe "-javaagent:D:JetBrainsDeveloperToolsIntelliJ IDEA 2021.2libidea_rt.jar=10944:D:JetBrainsDeveloperToolsIntelliJ IDEA 2021.2bin" -Dfile.encoding=UTF-8 -classpath "D:Java_JDKJDK8u202jrelibcharsets.jar;D:Java_JDKJDK8u202jrelibdeploy.jar;D:Java_JDKJDK8u202jrelibextaccess-bridge-64.jar;D:Java_JDKJDK8u202jrelibextcldrdata.jar;D:Java_JDKJDK8u202jrelibextdnsns.jar;D:Java_JDKJDK8u202jrelibextjaccess.jar;D:Java_JDKJDK8u202jrelibextjfxrt.jar;D:Java_JDKJDK8u202jrelibextlocaledata.jar;D:Java_JDKJDK8u202jrelibextnashorn.jar;D:Java_JDKJDK8u202jrelibextsunec.jar;D:Java_JDKJDK8u202jrelibextsunjce_provider.jar;D:Java_JDKJDK8u202jrelibextsunmscapi.jar;D:Java_JDKJDK8u202jrelibextsunpkcs11.jar;D:Java_JDKJDK8u202jrelibextzipfs.jar;D:Java_JDKJDK8u202jrelibjavaws.jar;D:Java_JDKJDK8u202jrelibjce.jar;D:Java_JDKJDK8u202jrelibjfr.jar;D:Java_JDKJDK8u202jrelibjfxswt.jar;D:Java_JDKJDK8u202jrelibjsse.jar;D:Java_JDKJDK8u202jrelibmanagement-agent.jar;D:Java_JDKJDK8u202jrelibplugin.jar;D:Java_JDKJDK8u202jrelibresources.jar;D:Java_JDKJDK8u202jrelibrt.jar;D:My ProjectsSpringMVCssm_demooutproductiontest;D:My ProjectsSpringMVCssm_demotestsrctopharmonytqslombok-1.18.20.jar" top.harmonytqs.Main
Person(name=张三, age=21, gender=男)

Process finished with exit code 0

Q.E.D.

0 人点赞