qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          Java實例化類的方法

           Java中,類的實例化方法有四種途徑:

            1)使用new操作符

            2)調(diào)用Class對象的newInstance()方法

            3)調(diào)用clone()方法,對現(xiàn)有實例的拷貝

            4)通過ObjectInputStream的readObject()方法反序列化類

            1、ClassInstance.java

          1. import java.io.*;  
          2.    
          3.  class ClassInstance implements Cloneable, Serializable {  
          4.      private String str = "測試...";  
          5.      public void fun(){  
          6.          System.out.println(str);  
          7.      }  
          8.      public ClassInstance(String str){  
          9.          System.out.println("有參類的實例化");  
          10.          this.str += str;   
          11.      }  
          12.      public ClassInstance(){  
          13.          System.out.println("無參類的實例化");  
          14.      }  
          15.      public Object clone(){  
          16.          return this;  
          17.      }  
          18.  }

            2、ClassInstanceTest.java

          1. import java.io.*;  
          2.  import java.lang.reflect.*;  
          3.    
          4.  public class ClassInstanceTest{  
          5.      public static void main(String[] args) throws ClassNotFoundException, InstantiationException,  
          6.             IllegalAccessException, IOException,InvocationTargetException, NoSuchMethodException{  
          7.          //第一種類的實例化方式 
          8.          ClassInstance ci01 = new ClassInstance("01");  
          9.          ci01.fun();  
          10.    
          11.          //第二種類的實例化方式 
          12.          ClassInstance ci02 = (ClassInstance) Class.forName("ClassInstance").newInstance();  
          13.          ci02.fun();  
          14.    
          15.          //第三種類的實例化方式 
          16.          ClassInstance ci03 = (ClassInstance) ci01.clone();  
          17.          ci03.fun();  
          18.    
          19.          //第四種類的實例化方式 
          20.          FileOutputStream fos = new FileOutputStream("ci.tmp");  
          21.          ObjectOutputStream oos = new ObjectOutputStream(fos);  
          22.          oos.writeObject(ci01);  
          23.          oos.close();  
          24.          fos.close();  
          25.            
          26.          FileInputStream fis = new FileInputStream("ci.tmp");  
          27.          ObjectInputStream ois = new ObjectInputStream(fis);  
          28.            
          29.          ClassInstance ci04  = (ClassInstance) ois.readObject();  
          30.          ois.close();  
          31.          fis.close();  
          32.            
          33.          ci04.fun();  
          34.          System.out.println("--------------------額外測試--------------------");  
          35.          ClassInstance ci05 = null;  
          36.          //額外的思考 在第二種類實例化的方式中有沒有一種方法實現(xiàn)有參數(shù)的構(gòu)造方式 
          37.          //獲得類的構(gòu)造信息 
          38.          Constructor[] ctor = Class.forName("ClassInstance").getDeclaredConstructors();  
          39.          //找到我們需要的構(gòu)造方法 
          40.          for(int i=0;i<ctor.length;i++ ){  
          41.              Class[] cl = ctor[i].getParameterTypes();  
          42.              if(cl.length == 1){  
          43.                  //實例化對象 
          44.                  ci05 = (ClassInstance) Class.forName("ClassInstance").getConstructor(cl).newInstance(new Object[]{"05"});  
          45.              }  
          46.          }  
          47.          ci05.fun();  
          48.      }  
          49.  }

            3、輸出結(jié)果

          1. 有參類的實例化  
          2.  測試...01  
          3.  無參類的實例化  
          4.  測試...  
          5.  測試...01  
          6.  測試...01  
          7.  -------------------額外測試--------------------  
          8.  有參類的實例化  
          9.  測試...05

            除了這幾種情況可以實例化一個Java類對象外,隱式調(diào)用實例化也是利用了已上集中情況。例如常見的方法:

          1. public class ClassInstance{  
          2.  public ClassInstance(){  
          3.  }  
          4.      public ClassInstance getInstance(){  
          5.  return new ClassInstance();  
          6.  }  
          7.  }

            通過觀察結(jié)果,我們發(fā)現(xiàn)無論哪一種方式必須經(jīng)過的一步---調(diào)用構(gòu)造方法。無論怎樣構(gòu)造函數(shù)作為初始化類的意義怎樣都不會改變。

          posted on 2012-05-25 10:15 順其自然EVO 閱讀(224) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          <2012年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 兴海县| 尖扎县| 琼结县| 新乐市| 武川县| 岳阳市| 台东县| 阳城县| 广昌县| 六安市| 德格县| 冷水江市| 南澳县| 揭阳市| 洞口县| 大足县| 筠连县| 高密市| 鄂尔多斯市| 利津县| 邢台市| 安岳县| 抚顺市| 洛隆县| 临颍县| 肃宁县| 丹寨县| 万全县| 荥阳市| 翁牛特旗| 神农架林区| 阿坝县| 大丰市| 大新县| 泾川县| 彩票| 博客| 玛纳斯县| 清水河县| 宁武县| 长治市|