return this的用法

 每次返回类实例,用return this;

 

  1. class Test2 {
  2. String s="";
  3. public static void main(String[] args) {
  4.    Test2 t = new Test2();
  5.    t.method("111");
  6.    t.method("222");
  7.    t.method("333");
  8.    t.method2();
  9. }
  10. public Test2 method(String sb) {
  11.    s+=sb;
  12.    return this;
  13. }
  14. public void method2() {
  15.   
  16.    System.out.println(s);
  17. }
  18. }

 结果: 111222333

 

  1. class Test 
  2. public static void main(String[] args) 
  3. Test t = new Test().method();//返回当前这个类的实例给t 
  4. t.method2(); 
  5. public Test method() 
  6. return this
  7. public void method2() 
  8. System.out.println( "Hello Beijing 2008! "); 

return this就是返回当前这个类的一个实例 this 就是代表当前这个类的一个实例.


 原文参考:

http://hi.baidu.com/rague/blog/item/5a44a882a0d1afa70cf4d2b9.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值