资讯   |   开发   |   选机中心   |   产品大全 | IBM | 惠普 | 联想 | 戴尔 | 苹果 | 神舟
更多: | 华硕 | 明基 | 方正 | 紫光 | TCL | 夏新 | 联宝 | 宏碁 | 七喜 | 长城 | 清华同方 | 海尔 | 三星 | 东芝 | 索尼 | 富士通 | LG | 技术 | ddnoon
当前位置:笔记本 > 软件开发 >
Advertisement
文章正文

菜鸟求教

类型:转载   责任编辑:asp.net   日期:2007/05/23


热门软件下载:


   

 
  1)     这个程序是用到了testthis.increment().increment().increntment(),使得i进行加3次的算   class   Testthis{  
                    private   int   i=0;  
                  Testhis   increment(){  
                 
        //   上面的定义是什么意思?  
                        i++;  
                    return   this;  
                    //最搞不懂还是这句,到底是返回了什么啊? 这里this应该代表了什么?  
                  }  
     
       
  2)  
            class   sub(){  
  ....         }  
   
            class   object{  
                      main()|  
                            sub   subA=new   sub;  
                            (new   object()).usesub(subA);  
                            //这里是个什么定义方法?   找了半天没找到,不过大概意思是知道的  
                              }  
            void   usesub(sub   x){  
              system.out.printkn(x.getx()):  
         
              }    
 

网友回答:

发表者:sysmaster

1、Testhis   increment(){}定义一个 Testhis 类型的方法,返回类型为Testhis  
  2、return   this; 指当前对象  
  3、(new   object()).usesub(subA); 实例化object类的一个对象,并调用它的usesub(subA)方法

发表者:tiantian335

这个程序在thinking   in   java里面有的,我最近刚看过  
  Testhis   increment(){  
  是说方法increment的返回类型是Testhis,既然需要testthis.increment().increment().increntment()这样调用反复调用increment,那每次调用后的返回值必须是个Testhis   对象句柄  
                   
  return   this;  
  即实现了返回Testhis   对象句柄,this是指当前对象的句柄,而为什么使用this呢?因为i不是static的,为了保证多次调用increment,i的值都在增加,那就必须返回当前正在使用的这个句柄,即保证每次调用increment都是当前对象中的i值在增加

发表者:tiantian335

(new   object()).usesub(subA);  
  等于是  
  object   obj   =   new   object();  
  obj.usesub(subA);  
  只是简化了  
  就如同  
  (new   Integer(100)).toString();  
  等于  
  Integer   x   =   new   Integer(100);  
  x.toString();


 

 
热门推荐笔记本: IBM笔记本
相关文章:
webmaster:popbb@126.com   最佳浏览:1024X768 MSIE
©2007 popbb.net All Rights Reserved