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