Loading

java连续调用

ref:【java】static方法与连续调用

 1 public class ReturnThis {
 2 
 3     public int age;
 4 
 5     public ReturnThis grow() {
 6         age++;
 7         return this; // 返回调用该方法的对象
 8     }
 9 
10     public static void main(String[] args) {
11         ReturnThis rt = new ReturnThis();
12         rt.grow().grow().grow();
13         System.out.println("rt的age 变量值是:" + rt.age);
14     }
15 
16 }

重点:返回该方法的对象this

 

posted @ 2022-05-10 13:28  ArkiWang  阅读(326)  评论(0编辑  收藏  举报