游泳的魚

          Class.forName()與new 的區(qū)別:

          Class.forName(xxx.xx.xx) 返回的是一個類, .newInstance() 后才創(chuàng)建一個對象 Class.forName(xxx.xx.xx);的作用是要求JVM查找并加載指定的類,也就是說JVM會執(zhí)行該類的靜態(tài)代碼段

          Class aClass = Class.forName(xxx.xx.xx);
          Object anInstance = aClass.newInstance();

          Class.forName("").newInstance()
          返回的是
          object

          but there is some limit for this method to create instance
          that is your class constructor should no contain parameters, and you should cast the instance manually.

           

          下面的例子:Class的最大作用就是實現(xiàn)了動態(tài)加載類,為多態(tài)提供了很好的幫助。

          class A
          {
              
          void aa()
              
          {
                  System.out.println ("A
          里的");    
                  
              }       
          }
          class B extends A
          {
              
          void aa()
              
          {
                  System.out.println ("B
          里的");    
                  
              }      
          }
          class C extends A
          {
              
          void aa()
              
          {
                  System.out.println ("C
          里的");       
              }     
          }


          public class ClassDemo
          {
              
              
          public static void main(String[] args)
              
          {
                  ClassDemo t=
          new ClassDemo();
                  t.show("C");
              }
              
          void show(String name)
              
          {
                  
          try
                  
          {
                       A show=(A)Class.forName(name).newInstance();
                       show.aa();
                  } 
          catch(Exception e){
                       e.printStackTrace();
                  }
               }
          }

          Class.forName()與new 的區(qū)別:

          jvm的角度看,我們使用new的時候,這個要new的類可以沒有加載;
            但是使用newInstance時候,就必須保證:1、這個類已經(jīng)加載;2、這個類已經(jīng)連接了。而完成上面兩個步驟的正是class的靜態(tài)方法forName()方法,這個靜態(tài)方法調(diào)用了啟動類加載器(就是加載java API的那個加載器)。
            有了上面jvm上的理解,那么我們可以這樣說,newInstance實際上是把new這個方式分解為兩步,即,首先調(diào)用class的加載方法加載某個類,然后實例化。
            這樣分步的好處是顯而易見的。我們可以在調(diào)用class的靜態(tài)加載方法forName時獲得更好的靈活性,提供給了我們降耦的手段。

          [
          補充:]
          newInstance:
          弱類型低效率,只能調(diào)用無參構(gòu)造。
          new:
          強類型,相對高效,能調(diào)用任何public構(gòu)造。


          posted on 2008-03-19 01:35 閱讀(420) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統(tǒng)計

          留言簿(1)

          我參與的團隊

          文章檔案

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 贺兰县| 上饶市| 郁南县| 海淀区| 改则县| 崇文区| 镇平县| 武鸣县| 疏勒县| 东丽区| 临汾市| 莱阳市| 太仓市| 达拉特旗| 宁国市| 进贤县| 永济市| 横山县| 万荣县| 仁怀市| 平顶山市| 兰溪市| 斗六市| 衡南县| 宁都县| 渭南市| 东台市| 黎平县| 卢龙县| 汉阴县| 临猗县| 广水市| 奇台县| 平陆县| 杨浦区| 桐乡市| 新沂市| 长宁区| 高碑店市| 大安市| 休宁县|