JAVA中实现链式操作(方法链)的简单例子

使用链式编程带来的简单

student

package jetty;

import java.util.stream.IntStream;

/**
 * @Auther: Xiao Yu
 * @Date: Created in 14:52 2018/3/22
 */
public class Student {
    private Integer id;
    private Integer age;
    private String  name;
    private  String  address;
    private  String  happy;

    public Student getId() {
        System.out.println(this.id);
        return  this;
    }

    public Student setId(Integer id) {
        this.id = id;
        return  this;
    }

    public Student getAge() {
        System.out.println(this.age);
        return  this;
    }

    public Student setAge(Integer age) {
        this.age = age;
        return  this;
    }

    public Student getName() {
        System.out.println(this.name);
        return  this;
    }

    public Student setName(String name) {
        this.name = name;
        return  this;
    }

    public Student getAddress() {
        System.out.println(this.address);
        return  this;
    }

    public Student setAddress(String address) {
        this.address = address;
        return  this;
    }

    public Student getHappy() {
        System.out.println(this.happy);
        return  this;
    }

    public Student setHappy(String happy) {
        this.happy = happy;
        return  this;
    }
}

Test

package jetty;

/**
 * @Auther: Xiao Yu
 * @Date: Created in 14:52 2018/3/22
 */
public class Test {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setId(1).setAge(12).setName("小红").setAddress("北京").setHappy("高兴");
        stu.getId().getAge().getName().getAddress().getHappy();
    }
}

控制台打印

E:jdkinjava -Didea.launcher.port=7534 "-Didea.launcher.bin.path=E:ideatIntelliJ IDEA 2016.3.2in" -Dfile.encoding=UTF-8 -classpath "E:jdkjrelibcharsets.jar;E:jdkjrelibdeploy.jar;E:jdkjrelibextaccess-bridge-32.jar;E:jdkjrelibextcldrdata.jar;E:jdkjrelibextdnsns.jar;E:jdkjrelibextjaccess.jar;E:jdkjrelibextjfxrt.jar;E:jdkjrelibextlocaledata.jar;E:jdkjrelibext
ashorn.jar;E:jdkjrelibextsunec.jar;E:jdkjrelibextsunjce_provider.jar;E:jdkjrelibextsunmscapi.jar;E:jdkjrelibextsunpkcs11.jar;E:jdkjrelibextzipfs.jar;E:jdkjrelibjavaws.jar;E:jdkjrelibjce.jar;E:jdkjrelibjfr.jar;E:jdkjrelibjfxswt.jar;E:jdkjrelibjsse.jar;E:jdkjrelibmanagement-agent.jar;E:jdkjrelibplugin.jar;E:jdkjrelib
esources.jar;E:jdkjrelib
t.jar;E:Y2166Quartz	argetclasses;E:maven
epositoryorgquartz-schedulerquartz2.2.3quartz-2.2.3.jar;E:maven
epositoryc3p0c3p0.9.1.1c3p0-0.9.1.1.jar;E:maven
epositoryorgslf4jslf4j-api1.7.7slf4j-api-1.7.7.jar;E:maven
epositoryorgquartz-schedulerquartz-jobs2.2.1quartz-jobs-2.2.1.jar;E:maven
epositoryorgspringframeworkspring-webmvc4.1.8.releasespring-webmvc-4.1.8.RELEASE.jar;E:maven
epositoryorgspringframeworkspring-beans4.1.8.RELEASEspring-beans-4.1.8.RELEASE.jar;E:maven
epositoryorgspringframeworkspring-expression4.1.8.RELEASEspring-expression-4.1.8.RELEASE.jar;E:maven
epositoryorgspringframeworkspring-web4.1.8.RELEASEspring-web-4.1.8.RELEASE.jar;E:maven
epositoryorgspringframeworkspring-context4.2.0.RELEASEspring-context-4.2.0.RELEASE.jar;E:maven
epositoryorgspringframeworkspring-aop4.1.6.RELEASEspring-aop-4.1.6.RELEASE.jar;E:maven
epositoryaopallianceaopalliance1.0aopalliance-1.0.jar;E:maven
epositoryorgspringframeworkspring-core4.1.6.RELEASEspring-core-4.1.6.RELEASE.jar;E:maven
epositorycommons-loggingcommons-logging1.2commons-logging-1.2.jar;E:maven
epositoryorgspringframeworkspring-context-support3.2.4.RELEASEspring-context-support-3.2.4.RELEASE.jar;E:maven
epositoryorgspringframeworkspring-tx4.0.3.RELEASEspring-tx-4.0.3.RELEASE.jar;E:ideatIntelliJ IDEA 2016.3.2libidea_rt.jar" com.intellij.rt.execution.application.AppMain jetty.Test
1
12
小红
北京
高兴

Process finished with exit code 0
原文地址:https://www.cnblogs.com/lcycn/p/8624012.html