网站建设期末作业,申请注册自媒体平台,网页打开app wordpress,wordpress侧边栏图和标题前言现在很多开源库或者代码都会使用链式调用。因为链式调用在很多时候#xff0c;都可以使我们的代码更加简洁易懂。以下Student类有多数个属性#xff0c;让我们看看非链式调用和链式调用有何区别。非链式调用Main类#xff1a;/*** Created by chenxuxu on 17-1-10.*/pub…前言现在很多开源库或者代码都会使用链式调用。因为链式调用在很多时候都可以使我们的代码更加简洁易懂。以下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;//...此处省略gettersetter}链式调用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;}}}结论通过链式调用后代码看起来简洁易懂。