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:\jdk\bin\java -Didea.launcher.port=7534 "-Didea.launcher.bin.path=E:\ideat\IntelliJ IDEA 2016.3.2\bin" -Dfile.encoding=UTF-8 -classpath "E:\jdk\jre\lib\charsets.jar;E:\jdk\jre\lib\deploy.jar;E:\jdk\jre\lib\ext\access-bridge-32.jar;E:\jdk\jre\lib\ext\cldrdata.jar;E:\jdk\jre\lib\ext\dnsns.jar;E:\jdk\jre\lib\ext\jaccess.jar;E:\jdk\jre\lib\ext\jfxrt.jar;E:\jdk\jre\lib\ext\localedata.jar;E:\jdk\jre\lib\ext\nashorn.jar;E:\jdk\jre\lib\ext\sunec.jar;E:\jdk\jre\lib\ext\sunjce_provider.jar;E:\jdk\jre\lib\ext\sunmscapi.jar;E:\jdk\jre\lib\ext\sunpkcs11.jar;E:\jdk\jre\lib\ext\zipfs.jar;E:\jdk\jre\lib\javaws.jar;E:\jdk\jre\lib\jce.jar;E:\jdk\jre\lib\jfr.jar;E:\jdk\jre\lib\jfxswt.jar;E:\jdk\jre\lib\jsse.jar;E:\jdk\jre\lib\management-agent.jar;E:\jdk\jre\lib\plugin.jar;E:\jdk\jre\lib\resources.jar;E:\jdk\jre\lib\rt.jar;E:\Y2166\Quartz\target\classes;E:\maven\repository\org\quartz-scheduler\quartz\2.2.3\quartz-2.2.3.jar;E:\maven\repository\c3p0\c3p0\0.9.1.1\c3p0-0.9.1.1.jar;E:\maven\repository\org\slf4j\slf4j-api\1.7.7\slf4j-api-1.7.7.jar;E:\maven\repository\org\quartz-scheduler\quartz-jobs\2.2.1\quartz-jobs-2.2.1.jar;E:\maven\repository\org\springframework\spring-webmvc\4.1.8.release\spring-webmvc-4.1.8.RELEASE.jar;E:\maven\repository\org\springframework\spring-beans\4.1.8.RELEASE\spring-beans-4.1.8.RELEASE.jar;E:\maven\repository\org\springframework\spring-expression\4.1.8.RELEASE\spring-expression-4.1.8.RELEASE.jar;E:\maven\repository\org\springframework\spring-web\4.1.8.RELEASE\spring-web-4.1.8.RELEASE.jar;E:\maven\repository\org\springframework\spring-context\4.2.0.RELEASE\spring-context-4.2.0.RELEASE.jar;E:\maven\repository\org\springframework\spring-aop\4.1.6.RELEASE\spring-aop-4.1.6.RELEASE.jar;E:\maven\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;E:\maven\repository\org\springframework\spring-core\4.1.6.RELEASE\spring-core-4.1.6.RELEASE.jar;E:\maven\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;E:\maven\repository\org\springframework\spring-context-support\3.2.4.RELEASE\spring-context-support-3.2.4.RELEASE.jar;E:\maven\repository\org\springframework\spring-tx\4.0.3.RELEASE\spring-tx-4.0.3.RELEASE.jar;E:\ideat\IntelliJ IDEA 2016.3.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain jetty.Test
1
12
小红
北京
高兴

Process finished with exit code 0

 

posted @ 2018-03-22 15:12  快乐的小银龙  阅读(656)  评论(0编辑  收藏  举报