<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國際加速解決方案。 廣告
                ### [類方法提取器](https://lingcoder.gitee.io/onjava8/#/book/19-Type-Information?id=%e7%b1%bb%e6%96%b9%e6%b3%95%e6%8f%90%e5%8f%96%e5%99%a8) 通常,你不會直接使用反射工具,但它們可以幫助你創建更多的動態代碼。反射是用來支持其他 Java 特性的,例如對象序列化(參見[附錄:對象序列化](https://lingcoder.github.io/OnJava8/#/book/Appendix-Object-Serialization))。但是,有時動態提取有關類的信息很有用。 考慮一個類方法提取器。查看類定義的源代碼或 JDK 文檔,只顯示*在該類定義中*定義或重寫的方法。但是,可能還有幾十個來自基類的可用方法。找到它們既單調又費時\[^1\]。幸運的是,反射提供了一種方法,可以簡單地編寫一個工具類自動地向你展示所有的接口: ~~~ // typeinfo/ShowMethods.java // 使用反射展示一個類的所有方法,甚至包括定義在基類中方法 // {java ShowMethods ShowMethods} import java.lang.reflect.*; import java.util.regex.*; public class ShowMethods { private static String usage = "usage:\n" + "ShowMethods qualified.class.name\n" + "To show all methods in class or:\n" + "ShowMethods qualified.class.name word\n" + "To search for methods involving 'word'"; private static Pattern p = Pattern.compile("\\w+\\."); public static void main(String[] args) { if (args.length < 1) { System.out.println(usage); System.exit(0); } int lines = 0; try { Class<?> c = Class.forName(args[0]); Method[] methods = c.getMethods(); Constructor[] ctors = c.getConstructors(); if (args.length == 1) { for (Method method : methods) System.out.println( p.matcher( method.toString()).replaceAll("")); for (Constructor ctor : ctors) System.out.println( p.matcher(ctor.toString()).replaceAll("")); lines = methods.length + ctors.length; } else { for (Method method : methods) if (method.toString().contains(args[1])) { System.out.println(p.matcher( method.toString()).replaceAll("")); lines++; } for (Constructor ctor : ctors) if (ctor.toString().contains(args[1])) { System.out.println(p.matcher( ctor.toString()).replaceAll("")); lines++; } } } catch (ClassNotFoundException e) { System.out.println("No such class: " + e); } } } ~~~ 輸出結果: ~~~ public static void main(String[]) public final void wait() throws InterruptedException public final void wait(long,int) throws InterruptedException public final native void wait(long) throws InterruptedException public boolean equals(Object) public String toString() public native int hashCode() public final native Class getClass() public final native void notify() public final native void notifyAll() public ShowMethods() ~~~ `Class`方法`getmethods()`和`getconstructors()`分別返回`Method`數組和`Constructor`數組。這些類中的每一個都有進一步的方法來解析它們所表示的方法的名稱、參數和返回值。但你也可以像這里所做的那樣,使用`toString()`,生成帶有整個方法簽名的`String`。代碼的其余部分提取命令行信息,確定特定簽名是否與目標`String`(使用`indexOf()`)匹配,并使用正則表達式(在[Strings](https://lingcoder.gitee.io/onjava8/#/book/19-Type-Informationstrings)一章中介紹)刪除名稱限定符。 編譯時無法知道`Class.forName()`生成的結果,因此所有方法簽名信息都是在運行時提取的。如果你研究 JDK 反射文檔,你將看到有足夠的支持來實際設置和對編譯時完全未知的對象進行方法調用(本書后面有這樣的例子)。雖然最初你可能認為你永遠都不需要這樣做,但是反射的全部價值可能會令人驚訝。 上面的輸出來自命令行: ~~~ java ShowMethods ShowMethods ~~~ 輸出包含一個`public`無參數構造函數,即使未定義構造函數。你看到的構造函數是由編譯器自動合成的。如果將`ShowMethods`設置為非`public`類(即只有包級訪問權),則合成的無參數構造函數將不再顯示在輸出中。自動為合成的無參數構造函數授予與類相同的訪問權。 嘗試運行`java ShowMethods java.lang.String`,并附加一個`char`、`int`、`String`等參數。 編程時,當你不記得某個類是否有特定的方法,并且不想在 JDK 文檔中搜索索引或類層次結構時,或者如果你不知道該類是否可以對`Color`對象執行任何操作時,該工具能節省不少時間。
                  <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>

                              哎呀哎呀视频在线观看