<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## 獲取Class對象 ### 使用 Class.forName 靜態方法。 當你知道該類的全路徑名時,你可以使用該方法獲取 Class 類對象。 ~~~ Class clz = Class.forName("java.lang.String"); ~~~ ### 使用 .class 方法 這種方法只適合在編譯前就知道操作的 Class。 ~~~ Class clz = String.class; ~~~ ### 使用類對象的 getClass() 方法。 ~~~ String str = new String("Hello"); Class clz = str.getClass(); ~~~ ## 創建類對象 ### 通過 Class 對象的 newInstance() 方法。 ~~~ Class clz = Apple.class; Apple apple = (Apple)clz.newInstance(); ~~~ ### 通過 Constructor 對象的 newInstance() 方法 ~~~ Class clz = Apple.class; Constructor constructor = clz.getConstructor(); Apple apple = (Apple)constructor.newInstance(); ~~~ 通過 Constructor 對象創建類對象可以選擇特定構造方法,而通過 Class 對象則只能使用默認的無參數構造方法。下面的代碼就調用了一個有參數的構造方法進行了類對象的初始化。 ~~~ Class clz = Apple.class; Constructor constructor = clz.getConstructor(String.class, int.class); Apple apple = (Apple)constructor.newInstance("紅富士", 15); ~~~ ## 反射獲取類屬性、方法、構造器 * 通過 Class 對象的 getFields() 方法可以獲取 Class 類的屬性,但無法獲取私有屬性。 * 通過Class 對象的getDeclaredFields() 方法則可以獲取包括私有屬性在內的所有屬性。 * 與獲取類屬性一樣,當我們去獲取類方法、類構造器時,如果要獲取私有方法或私有構造器,則必須使用有 declared 關鍵字的方法。 ## 推薦閱讀 [深入解析Java反射(1) - 基礎](https://www.sczyh30.com/posts/Java/java-reflection-1/) [大白話說Java反射:入門、使用、原理](https://www.cnblogs.com/chanshuyi/p/head_first_of_reflection.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看