This article describes the commonly used chain call writing methods in Java and Android. Share it for your reference, as follows:
Recently, it was discovered that in many of the popular open source frameworks, most of them use the form of "(method).(method).(method)" to call, the most typical of which is RxJava. The source code of the AlertDialog control in Android is also in this form. After reading, we can see that everyone calls it a chain call . "Action is the only criterion for inspection procedures" 0.0! After checking and saying so much, you still have to write an example and run it to achieve the expected results.
/** * * Chained call* * @author kk * */public class Student { public Student() { } public static Builder builder() { return new Builder(); } // Static inner class static class Builder { /* Name*/ private String name; /* Age*/ private String age; /* Grade*/ private String grade; /* Student ID*/ private String no; /* Major*/ private String Professional; public String getName() { return name; } public Builder setName(String name) { this.name = name; return this; } public String getAge() { return age; } public Builder setAge(String age) { this.age = age; return this; } public String getGrade() { return grade; } public Builder setGrade(String grade) { this.grade = grade; return this; } public String getNo() { return no; } public Builder setNo(String no) { this.no = no; return this; } public String getProfessional() { return Professional; } public Builder setProfessional(String professional) { Professional = professional; return this; } public void showMessagwe() { System.out.println("Name:" + this.name); System.out.println("Age:" + this.age); System.out.println("Class:" + this.grade); System.out.println("Student number:" + this.no); System.out.println("Professional:" + this.Professional); } } public static void main(String[] args) { // Chain call (concise code, strong readability) Student.builder().setName("There are always troublemakers who want to harm me").setAge("23").setGrade("Junior Three") .setNo("20110310").setProfessional("Information Management and Information System").showMessagwe(); }}Running results:
For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.