java 链式调用

前言

现在很多开源库或者代码都会使用链式调用。因为链式调用在很多时候,都可以使我们的代码更加简洁易懂。以下 Student 类有多数个属性,让我们看看非链式调用和链式调用有何区别。

非链式调用

Main 类:

    /**
     * Created by chenxuxu on 17-1-10.
     */
    public class Main {
        public static void main(String[] args) {
            Student stu = new Student();
            stu.setAge(22);
            stu.setName("chenxuxu");
            stu.setGrade("13级");
            stu.setNo("123456789");
            stu.setMajor("软件工程");
        }
    }

Student 类:


    /**
     * 学生类
     * 
     * Created by chenxuxu on 17-1-10.
     */
    public class Student {
        /**
         * 姓名
         */
        private String name;
        /**
         * 年龄
         */
        private int age;
        /**
         * 学号
         */
        private String no;
        /**
         * 年级
         */
        private String grade;
        /**
         * 专业
         */
        private String major;

        //...此处省略getter&setter
    }

链式调用

Main 类:

    /**
     * Created by chenxuxu on 17-1-10.
     */
    public class Main {
        public static void main(String[] args) {
            Student.builder()
            .stuName("chenxuxu")
            .stuAge(22)
            .stuGrade("13级")
            .stuMajor("软件工程")
            .stuNo("123456789");
        }
    }

Student 类:


    /**
     * 学生类
     * 
     * Created by chenxuxu on 17-1-10.
     */
    public class Student {
        /**
         * 不能通过new初始化
         */
        private Student(){}

        public static Builder builder(){
            return new Builder();
        } 

        static class Builder{
            /**
             * 姓名
             */
            private String name;
            /**
             * 年龄
             */
            private int age;
            /**
             * 学号
             */
            private String no;
            /**
             * 年级
             */
            private String grade;
            /**
             * 专业
             */
            private String major;

            public Builder stuName(String name){
                this.name = name;
                return this;
            }

            public Builder stuAge(int age){
                this.age = age;
                return this;
            }

            public Builder stuNo(String no){
                this.no = no;
                return this;
            }

            public Builder stuGrade(String grade){
                this.grade = grade;
                return this;
            }

            public Builder stuMajor(String major){
                this.major = major;
                return this;
            }
        }

    }

结论

通过链式调用后,代码看起来简洁易懂。

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值