posts - 11,  comments - 0,  trackbacks - 0

           

          在java程序中每一個類都有一個Class對象,被保存在同名的.Class對象當中,JVM會使用類加載器加載Class文件生成類的對象信息.

          當我們創建一個類的對象或者調用這個對象的靜態方法,jvm會自動加載類的對象信息

          獲得類的對象信息

          我們一般常用用兩種方式獲得類的對象信息.

          1. 我們可以用Class.forName()方法動態的根據類名獲得一個類的Class對象.

            1:     /**
            2:      * 使用此方法為自動初始化靜態變量和執行static塊的代碼
            3:      * 如果找不到對象會拋出一個ClassNotFoundException
            4:      */
            5:     Class second = Class.forName("classLoad.Second");

          2. 我們還可以用Class class = ClassName.class 來直接獲取一個類的類型信息,但用它和Class.forName()獲取類的對象

          引用信息在靜態塊時的執行時間上不同,看下面的例子:

          一個普通類信息

            1: interface Interface{
            2:   public static String interFlag = "接口";
            3:   
            4: }
            5: 
            6: class Parent implements Interface{
            7:   public static String parntFLAG = "父類靜態變量";
            8:   static{
            9:     final  String flag2 = "flag2";//static 區中只能用final修飾
           10:     System.out.println("我在父類靜態區里面!"+parntFLAG);
           11:   }
           12:   
           13:   public  Parent(){
           14:     System.out.println("我在父類構造函數里面!");
           15:   }
           16: }
           17: 
           18: 
           19: class Son extends  Parent{
           20:   public static String sonFLAG = "子類類靜態變量";
           21:   static{
           22:     System.out.println("我在子類靜態區里面!"+sonFLAG);
           23:   }
           24:   
           25:   public Son(){
           26:     System.out.println("我在子類構造函數里面!");
           27:   }
           28: }
            1: interface Interface{
            2:   public static String interFlag = "接口";
            3:   
            4: }
            5: 
            6: class Parent implements Interface{
            7:   public static String parntFLAG = "父類靜態變量";
            8:   static{
            9:     final  String flag2 = "flag2";//static 區中只能用final修飾
           10:     System.out.println("我在父類靜態區里面!"+parntFLAG);
           11:   }
           12:   
           13:   public  Parent(){
           14:     System.out.println("我在父類構造函數里面!");
           15:   }
           16: }
           17: 
           18: 
           19: class Son extends  Parent{
           20:   public static String sonFLAG = "子類類靜態變量";
           21:   static{
           22:     System.out.println("我在子類靜態區里面!"+sonFLAG);
           23:   }
           24:   
           25:   public Son(){
           26:     System.out.println("我在子類構造函數里面!");
           27:   }
           28: }

          使用這種方法引用類的對象不會始化靜態變量和執行靜態塊信息,這些代碼方法會在首次引用時執行.

          如下引用父類的靜態變量,不會執行子類的靜態塊.

            1: public class ClassLoad {
            2:   public static void main(String[] args) throws ClassNotFoundException  {
            3:     
            4:     //Son son = new Son();
            5:     /**
            6:      * 我們使用一個類的時候需要進行以下3項工作.
            7:      * 1.加載,
            8:      * 2.鏈接
            9:      * 3.初始化,此步會初始化靜態變量和執行靜態塊信息,但是這種方法會在
           10:      * 真正調用方法時執行
           11:      */
           12:     Class son = Son.class;
           13:     System.out.println("靜態區的初始化會在調用時執行!");
           14:     // parntFLAG 是父類的靜態變量 
           15:     // 此處只會執行父類的靜態快
           16:     System.out.println(Son.parntFLAG);
           17: 
           18:     /**
           19:      * 運行結果
           20:      *靜態區的初始化會在調用時執行!
           21:      *我在父類靜態區里面!父類靜態變量
           22:      *父類靜態變量
           23:      */
           24:   }
           25: }

          打印子類的靜態變量,所有的代碼都會執行

            1: 
            2: public class ClassLoad {
            3:   public static void main(String[] args) throws ClassNotFoundException  {
            4:     
            5:     //Son son = new Son();
            6:     /**
            7:      * 我們使用一個類的時候需要進行以下3項工作.
            8:      * 1.加載,
            9:      * 2.鏈接
           10:      * 3.初始化,此步會初始化靜態變量和執行靜態塊信息,但是這種方法會在
           11:      * 真正調用方法時執行
           12:      */
           13:     Class son = Son.class;
           14:     System.out.println("靜態區的初始化會在調用時執行!");
           15:     // parntFLAG 是父類的靜態變量 
           16:     // 此處只會執行父類的靜態快
           17:     System.out.println(Son.sonFLAG);
           18: 
           19:     /**
           20:      * 運行結果
           21:      * 靜態區的初始化會在調用時執行!
           22:      * 我在父類靜態區里面!父類靜態變量
           23:      * 我在子類靜態區里面!子類類靜態變量
           24:      * 子類類靜態變量
           25:      */
           26:   }
           27: }

           

          但是使用Class.forName 類加載時就會完成初始化工作.

            1: public class ClassLoad {
            2:   public static void main(String[] args) throws Exception  {
            3:     /**
            4:      * 使用Class.forName會自動加載所有靜態區的信息
            5:      */
            6:     Class son = Class.forName("classLoad.Son");
            7:     Son instance = (Son)son.newInstance();
            8:     /*
            9:      * 執行結果
           10:      * 我在父類靜態區里面!父類靜態變量
           11:      * 我在子類靜態區里面!子類類靜態變量
           12:      * 我在父類構造函數里面!
           13:      * 我在子類構造函數里面!
           14:      */  
           15: 
           16:   }
           17: }
          posted @ 2011-03-13 18:41 小暉 閱讀(890) | 評論 (0)編輯 收藏

          該類提供了線程局部 (thread-local) 變量。這些變量不同于它們的普通對應物,因為訪問某個變量(通過其 get 或 set 方法)的每個線程都有自己的局部變量,它獨立于變量的初始化副本。ThreadLocal 實例通常是類中的 private static 字段,它們希望將狀態與某一個線程(例如,用戶 ID 或事務 ID)相關聯。

          這個是什么,就是解決一個線程內共享一個變量,這個變量只在這個線程內部有效,在一個線程內訪問的都一個同一個對象,而多個線程之間的這個對象卻是相互獨立的。說來就是每一個線程都有一個獨立的此線程副本。
          使用ThreadLocal一般都聲明為靜態的變量
          在我們的線程中有一個threadLocals的Hash表來存放這個對象,我們用ThreadLocal對象作為主鍵,因此我們的ThreadLocal對象聲明為靜態的

          我們可以使用多個TheadLocal,來使一個線程里有多個共享的變量
          這個就是要實現一個線程里面共享一個變量

          public void set(Object value) {        
          	  Thread t = Thread.currentThread();
                  ThreadLocalMap map = getMap(t);
                  if (map != null) 
                      map.set(this, value);
                  else
                      createMap(t, value);
              }

           

              ThreadLocalMap getMap(Thread t) {
                  return t.threadLocals;
              }
          posted @ 2009-05-14 21:03 小暉 閱讀(210) | 評論 (0)編輯 收藏

           

          1. 添加帶有數據值 (< 次數秒 >) ReceiveTimeout DWORD 值 *: 在以下注冊表項中

          HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
          例如, 如果希望超時期間將 8 分鐘, ReceiveTimeout 數據值設置為 480000 (< 480 > 1000) 。

           
          2. 重新啟動計算機。
          詳情

          posted @ 2009-05-14 20:56 小暉 閱讀(1087) | 評論 (0)編輯 收藏
          LiveJournal Tags:

          MICROSOFT         PB(16Bit)         PB(32Bit)

          Bool                    Boolean         Boolean

          Char*                 Ref string       Ref String

          Colorref               Uint             Ulong

          Dword                 Uint             Ulong

          Handle                Uint             Ulong

          Hdc                    Uint             Ulong

          Hfile                   Uint             Ulong

          Hinstance           Uint             Ulong

          Hwnd                 Uint             Ulong

          Int                     Int             Int

          Lparam               Uint             Ulong

          Lpbyte               Ref Int         Ref Long

          Lpdword             Ref Uint        Ref Ulong

          Lpfiletime           Ref Time        Ref Time

          Lpint                 Ref Int          Ref Long

          Lpstr,Lpststr       Ref String     Ref String

          Lpvoid               Ref Structstruct_inst         Ref Struct struct_inst

          Mcierror            Long             Long

          Lpstr,Lpststr       Ref String     Ref String

          Lpvoid              Ref Structstruct_inst         Ref Struct struct_inst

          Pbyte               Ref Int[#]     Ref Long[#]

          Short               Int         Int

          Structure         Ref Struct struct_inst       Ref Struct Struct_inst

          Uint                Uint              Uint

          Void**           SUBROUTINE    SUBROUTINE

          Word                         Int       Ulong

           

          LiveJournal Tags:

           

          Catch0 

          pb能使用的必須是標準winapi即pasical 壓棧順序

          1. 制作dll文件時 需要stdcal
          2. 在函數中加入winapi

          eg:

          DLLIMPORT WINAPI int CI_MACForPb(
                        long           nFunction,      
                        unsigned char  *pEntity,     
                        unsigned char  *pInData,
                        unsigned long  nInLength,
                        unsigned char  *pMAC,
                        unsigned long  *pnMACLength
                        )
          posted @ 2009-05-14 20:48 小暉 閱讀(1758) | 評論 (0)編輯 收藏
          僅列出標題  下一頁

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          相冊

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 禹州市| 平果县| 灵丘县| 宁陵县| 启东市| 南木林县| 昆明市| 肇东市| 清远市| 葫芦岛市| 政和县| 梧州市| 谷城县| 南川市| 六枝特区| 红原县| 从江县| 拉萨市| 平安县| 兰州市| 象州县| 嵊泗县| 大荔县| 芜湖市| 囊谦县| 新沂市| 康马县| 安吉县| 独山县| 涿鹿县| 渑池县| 施甸县| 靖宇县| 利津县| 锦屏县| 白银市| 介休市| 望江县| 泰州市| 全椒县| 高州市|