<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 類加載機制與反射 ## 反射機制的概念 反射是 Java 中一個非常強大的工具,能夠方便我們創建更加靈活的代碼,這些代碼可以在運行時進行裝載。 類中有什么信息,利用反射機制就能獲得什么信息,不過有前提,必須知道類的全名。 > 但是使用反射對系統的開銷是比較大的。 ## 反射機制的作用 1. 在運行時判斷一個對象所屬的類; 2. 在運行時獲取類的對象; 3. 在運行時訪問 Java 對象的屬性、方法、構造器等等。 ## 反射機制的優點和缺點 * 靜態編譯:在編譯時就確定類型,綁定對象; * 動態編譯:運行時確定類型,綁定對象。動態編譯極大限度的發揮了Java的靈活性,體現了多態的概念,用于降低類之間的耦合; * 反射機制的優點:可以實現動態創建對象和編譯; * 反射機制的缺點:對性能有影響。反射基本上是一種解釋性的操作。我們告訴JVM,希望它做什么并滿足我們的要求,這類操作總是慢于只直接執行相同的操作。 ## 反射機制的示例 **通過一個對象獲得一個完整的包名和類名** ~~~ public class Client2 { public static void main(String[] args) { Demo demo = new Demo(); // 根據對象獲取完整的包名和類名 System.out.println(demo.getClass().getName()); } } class Demo2 { } ~~~ **實例化Class類對象** ~~~ public class Client3 { public static void main(String[] args) throws ClassNotFoundException { // Class類是所有對象的元數據的類對象 Class<?> demo1 = null; Class<?> demo2 = null; Class<?> demo3 = null; demo1 = Class.forName("com.ntqingniao.reflect.Demo3"); demo2 = new Demo3().getClass(); demo3 = Demo3.class; System.out.println(demo1.getName()); System.out.println(demo2.getName()); System.out.println(demo3.getName()); } } class Demo3 { } ~~~ **通過Class實例化其他類的對象** ~~~ package com.ntqingniao.reflect; public class Client4 { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { Class<?> demo = null; // 通過類全名創建Class對象(類型的工廠) demo = Class.forName("com.ntqingniao.reflect.Person"); // 調用工廠的newInstance()方法創建對象 Person per = (Person)demo.newInstance(); per.setName("Tom"); System.out.println(per); } } class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [" + (name != null ? "name=" + name + ", " : "") + "age=" + age + "]"; } } ~~~ > 如果把Person類中的無參構造函數取消,就會出現錯誤。 > 所以在編寫使用Class實例化其他類對象的時候,一定要定義一個無參的構造器。 **通過Class調用類中的構造器** ~~~ // 獲取該類的所有構造器對象 Constructor<?> cons[] = demo.getConstructors(); // 通過構造器對象的newInstance(Object.. params)可變參數方法構建對象 Person per1 = (Person)cons[0].newInstance(); Person per2 = (Person)cons[1].newInstance("Tom", 20); System.out.println(per1); System.out.println(per2); ~~~ **返回一個類實現的接口和繼承的父類** ~~~ // 獲取類的所有接口和直接父類方法 Class<?> inters[] = demo.getInterfaces(); Class<?> supers = demo.getSuperclass(); ~~~ **獲取類的全部屬性** ~~~ // 獲取類的全部屬性 Field[] fields = demo.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { // 獲取權限修飾符 int mo = fields[i].getModifiers(); String priv = Modifier.toString(mo); // 數據類型 Class<?> type = fields[i].getType(); // 屬性變量名 String name = fields[i].getName(); System.out.println(priv + " " + type.getName() + " " + name ) ; } ~~~ **調用類的方法** ~~~ Method method = demo.getMethod("getName"); method.invoke(demo.newInstance()); Person p = new Person(); Method method2 = demo.getMethod("getName", String.class); method2.invoke(p, "Reflect"); p.getName("Reflect"); ~~~ **調用類的Getter和Setter** ~~~ public class Client5 { public static void main(String[] args) throws Exception { Human tom = new Human(); setter(tom, "Name", "Tom", String.class); getter(tom, "Name"); System.out.println(tom); } public static void setter(Object obj, String attr, Object value, Class<?> type) throws Exception { Method method = obj.getClass().getMethod("set" + attr, type); method.invoke(obj, value); } public static void getter(Object obj, String attr) throws Exception { Method method = obj.getClass().getMethod("get" + attr); System.out.println(method.invoke(obj)); } } class Human { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Human [" + (name != null ? "name=" + name + ", " : "") + "age=" + age + "]"; } } ~~~ ## IOC原理 **設計模式之—工廠模式** 帶配置文件的工廠模式設計 ~~~ //生產水果的工廠 public class FactoryReflect2 { public static Fruit getInstance(String key) { Fruit fruit = null; try { fruit = (Fruit) Class.forName(Init.getValue(key)).newInstance(); } catch (Exception e) { e.printStackTrace(); } return fruit; } public static void main(String[] args) { Fruit fruit = FactoryReflect2.getInstance("orange"); fruit.eat(); } } class Init { private static Properties pro = null; static { pro = new Properties(); try { pro.load(Init.class.getClassLoader().getResourceAsStream("fruit.properties")); } catch (Exception e) { // pro.setProperty("apple", "com.ntqingniao.ioc.Apple"); // pro.setProperty("orange", "com.ntqingniao.ioc.Orange"); // pro.setProperty("banana", "com.ntqingniao.ioc.Banana"); } } public static String getValue(String key) { return pro.getProperty(key); } } ~~~
                  <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>

                              哎呀哎呀视频在线观看