Java链式方法

  在使用jQuery时候,我们经常会看到或者使用到方法链,例如:  

1 $("#p1").css("color","red").slideUp(2000).slideDown(2000);

  这段代码的意思是选择器选择id为p1的html标签,颜色变红,之后slideup,再slidedown。

  自然是可以将这几个方法分开写,但是不分开的话不仅有很好的易读性,代码量也会有减少,那么何乐而不为?这种调用函数的方式在jQuery中称为“Chaining”,原理也很简单:能够chaining的方法调用后返回对象本身即可。

  下面用java代码做个演示:

Person.java

 1 public class Persion {
 2     private int id;
 3     private String name;
 4     private String phoneNumber;
 5     private String address;
 6     public  Persion() {
 7     }
 8     public void setId(int id) { 
 9         this.id = id;
10     }
11     public void setName(String name) {
12         this.name = name;
13     }
14     public void setPhoneNumber(String phoneNumber) {
15         this.phoneNumber = phoneNumber;
16     }
17     public void setAddress(String address) {
18         this.address = address;
19     }
20     public void printId() {
21         System.out.println(this.id);
22     }
23     public void printName() {
24         System.out.println(this.name);
25     }
26     public void printPhoneNumber() {
27         System.out.println(this.phoneNumber);
28     }
29     public void printAddress() {
30         System.out.println(this.address);
31     }
32 }

  Test.java

 1 public class Test {
 2     public static void main(String[] args) {
 3         Persion persion1 = new Persion();
 4         persion1.setId(3);
 5         persion1.setName("John");
 6         persion1.setPhoneNumber("1111111");
 7         persion1.setAddress("US");
 8         persion1.printId();
 9         persion1.printName(); 
10         persion1.printPhoneNumber();
11         persion1.printAddress();
12     }
13 }

  使用chaining:

  Person.java

 1 public class Persion {
 2     private int id;
 3     private String name;
 4     private String phoneNumber;
 5     private String address;
 6     public  Persion() {
 7     }
 8     public Persion setId(int id) { 
 9         this.id = id;
10         return this;
11     }
12     public Persion setName(String name) {
13         this.name = name;
14         return this;
15     }
16     public Persion setPhoneNumber(String phoneNumber) {
17         this.phoneNumber = phoneNumber;
18         return this;
19     }
20     public Persion setAddress(String address) {
21         this.address = address;
22         return this;
23     }
24     public Persion printId() {
25         System.out.println(this.id);
26         return this;
27     }
28     public Persion printName() {
29         System.out.println(this.name);
30         return this;
31     }
32     public Persion printPhoneNumber() {
33         System.out.println(this.phoneNumber);
34         return this;
35     }
36     public Persion printAddress() {
37         System.out.println(this.address);
38         return this;
39     }
40 }

  Test.java:

 1 public class Test {
 2     public static void main(String[] args) {
 3         Persion persion1 = new Persion();
 4         persion1.setId(3).setName("John")
 5                 .setPhoneNumber("1111111").setAddress("US");
 6         persion1.printId()
 7                 .printName()
 8                 .printPhoneNumber()
 9                 .printAddress();
10     }
11 }

   链式方法,就是在除最后一个方法外,每个方法都返回一个对象或者调用同一个对象的多个方法。这种编程方式:编程性强;可读性强;代码简洁。但是不易于调试。

 

posted @ 2017-03-13 21:02  ngulc  阅读(1958)  评论(0编辑  收藏  举报