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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Java 反射 > 原文: [https://www.programiz.com/java-programming/reflection](https://www.programiz.com/java-programming/reflection) #### 在本教程中,我們將學習反射,這是 Java 編程中的一項功能,它使我們能夠檢查和修改類,方法等。 在 Java 中,反射使我們能夠在運行時檢查和操作類,接口,構造器,方法和字段。 * * * ## Java `Class`類 在學習 Java 反射之前,我們需要了解一個名為`Class`的 Java 類。 Java 中有一個名為`Class`的類,可在運行時保留有關對象和類的所有信息。 `Class`的對象描述特定類的屬性。 該對象用于執行反射。 * * * ## 創建名為`Class`的類對象 我們可以通過創建`Class`的對象 * **使用`forName()`方法** `forName()`接受字符串參數(類的名稱)并返回`Class`的對象。 返回的對象引用由字符串指定的類。 例如, ```java Class Dog { } Class c1 = Class.forName("Dog"); ``` * **使用`getClass()`方法** `getClass()`方法使用特定類的對象創建`Class`的新對象。 例如, ```java Dog d1 = new Dog() Class c1 = d1.getClass(); ``` * **使用`.class`** 我們還可以使用`.class`擴展名創建`Class`的對象。 例如, ```java Class c1 = Dog.class; ``` 創建`Class`的對象后,我們可以使用這些對象進行反射。 * * * ## 獲取接口 我們可以使用`Class`的`getInterfaces()`方法來收集有關由該類實現的接口的信息。 此方法返回接口數組。 ### 示例:獲取接口 ```java import java.lang.Class; import java.lang.reflect.*; interface Animal { public void display(); } interface Mammal { public void makeSound(); } class Dog implements Animal, Mammal { public void display() { System.out.println("I am a dog."); } public void makeSound() { System.out.println("Bark bark"); } } class ReflectionDemo { public static void main(String[] args) { try { // create an object of Dog class Dog d1 = new Dog(); // create an object of Class using getClass() Class obj = d1.getClass(); // find the interfaces implemented by Dog Class[] objInterface = obj.getInterfaces(); for(Class c : objInterface) { // print the name of interfaces System.out.println("Interface Name: " + c.getName()); } } catch(Exception e) { e.printStackTrace(); } } } ``` **輸出** ```java Interface Name: Animal Interface Name: Mammal ``` * * * ## 獲取超類和訪問修飾符 類`Class`的方法`getSuperclass()`可用于獲取有關特定類超類的信息。 而且,`Class`提供了一種方法`getModifier()`,該方法以整數形式返回類的修飾符。 ### 示例:獲取超類和訪問修飾符 ```java import java.lang.Class; import java.lang.reflect.*; interface Animal { public void display(); } public class Dog implements Animal { public void display() { System.out.println("I am a dog."); } } class ReflectionDemo { public static void main(String[] args) { try { // create an object of Dog class Dog d1 = new Dog(); // create an object of Class using getClass() Class obj = d1.getClass(); // Get the access modifier of Dog in integer form int modifier = obj.getModifiers(); System.out.println("Modifier: " + Modifier.toString(modifier)); // Find the superclass of Dog Class superClass = obj.getSuperclass(); System.out.println("Superclass: " + superClass.getName()); } catch(Exception e) { e.printStackTrace(); } } } ``` **輸出** ```java Modifier: public Superclass: Animal ``` 要了解`Class`的更多方法,請訪問 [java.lang.Class](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html) 。 * * * ## 反射字段,方法和構造器 包`java.lang.reflect`提供了可用于操縱類成員的類。 例如, * **`Method`類** - 提供有關類中方法的信息 * **`Field`類** - 提供有關類中字段的信息 * **`Constructor`類** - 提供有關類中構造器的信息 * * * ## 反射字段 我們可以使用`Field`類提供的各種方法來檢查和修改類的不同字段。 * **`getFields()`** - 返回該類及其超類的所有公共字段 * **`getDeclaredFields()`** - 返回類的所有字段 * **`getModifier()`** - 以整數形式返回字段的修飾符 * **`set(classObject, value)`** - 使用指定的值設置字段的值 * **`get(classObject)`** - 獲取字段的值 * **`setAccessible(boolean)`** - 使私有字段可訪問 **注意**:如果我們知道字段名稱,則可以使用 * **`getField("fieldName")`** - 從類返回名稱為`fieldName`的公共字段。 * **`getDeclaredField("fieldName")`** - 從類返回名稱為`fieldName`的字段。 要了解`Field`類的更多方法,請訪問[字段類](https://docs.oracle.com/javase/8/docs/api/index.html)。 ### 示例:訪問公共字段 ```java import java.lang.Class; import java.lang.reflect.*; class Dog { public String type; } class ReflectionDemo { public static void main(String[] args) { try{ Dog d1 = new Dog(); // create an object of the class Class Class obj = d1.getClass(); // manipulating the public field type of Dog Field field1 = obj.getField("type"); // set the value of field field1.set(d1, "labrador"); // get the value of field by converting in String String typeValue = (String)field1.get(d1); System.out.println("type: " + typeValue); // get the access modifier of type int mod1 = field1.getModifiers(); String modifier1 = Modifier.toString(mod1); System.out.println("Modifier: " + modifier1); System.out.println(" "); } catch(Exception e) { e.printStackTrace(); } } } ``` **輸出**: ```java type: labrador Modifier: public ``` ### 示例:訪問私有字段 ```java import java.lang.Class; import java.lang.reflect.*; class Dog { private String color; } class ReflectionDemo { public static void main(String[] args) { try { Dog d1 = new Dog(); // create an object of the class Class Class obj = d1.getClass(); // accessing the private field Field field2 = obj.getDeclaredField("color"); // making the private field accessible field2.setAccessible(true); // set the value of color field2.set(d1, "brown"); // get the value of type converting in String String colorValue = (String)field2.get(d1); System.out.println("color: " + colorValue); // get the access modifier of color int mod2 = field2.getModifiers(); String modifier2 = Modifier.toString(mod2); System.out.println("modifier: " + modifier2); } catch(Exception e) { e.printStackTrace(); } } } ``` **輸出**: ```java color: brown modifier: private ``` * * * ## Java 方法的反射 像字段一樣,我們可以使用`Method`類提供的各種方法來檢查類的不同方法。 * **`getMethods()`** - 返回該類及其超類的所有公共方法 * **`getDeclaredMethod()`** - 返回該類的所有方法 * **`getName()`** - 返回方法的名稱 * **`getModifiers()`** - 以整數形式返回方法的訪問修飾符 * **`getReturnType()`** - 返回方法的返回類型 要了解有關`Method`類的更多方法的信息,請訪問[方法類](https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html)。 ### 示例:方法反射 ```java import java.lang.Class; import java.lang.reflect.*; class Dog { public void display() { System.out.println("I am a dog."); } protected void eat() { System.out.println("I eat dog food."); } private void makeSound() { System.out.println("Bark Bark"); } } class ReflectionDemo { public static void main(String[] args) { try { Dog d1 = new Dog(); // create an object of Class Class obj = d1.getClass(); // get all the methods using the getDeclaredMethod() Method[] methods = obj.getDeclaredMethods(); // get the name of methods for(Method m : methods) { System.out.println("Method Name: " + m.getName()); // get the access modifier of methods int modifier = m.getModifiers(); System.out.println("Modifier: " + Modifier.toString(modifier)); // get the return types of method System.out.println("Return Types: " + m.getReturnType()); System.out.println(" "); } } catch(Exception e) { e.printStackTrace(); } } } ``` **輸出**: ```java Method Name: display Modifier: public Return type: void Method Name: eat Modifier: protected Return Type: void Method Name: makeSound Modifier: private Return Type: void ``` * * * ## 構造器的反射 我們還可以使用`Constructor`類提供的各種方法來檢查類的不同構造器。 * **`getConstructors()`** - 返回該類的所有公共構造器以及該類的超類 * **`getDeclaredConstructor()`** - 返回所有構造器 * **`getName()`** - 返回構造器的名稱 * **`getModifiers()`** - 以整數形式返回構造器的訪問修飾符 * **`getParameterCount()`** - 返回構造器的參數數量 要了解`Constructor`類的更多方法,請訪問[構造器類](https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Constructor.html) ### 示例:構造器反射 ```java import java.lang.Class; import java.lang.reflect.*; class Dog { public Dog() { } public Dog(int age) { } private Dog(String sound, String type) { } } class ReflectionDemo { public static void main(String[] args) { try { Dog d1 = new Dog(); Class obj = d1.getClass(); // get all the constructors in a class using getDeclaredConstructor() Constructor[] constructors = obj.getDeclaredConstructors(); for(Constructor c : constructors) { // get names of constructors System.out.println("Constructor Name: " + c.getName()); // get access modifier of constructors int modifier = c.getModifiers(); System.out.println("Modifier: " + Modifier.toString(modifier)); // get the number of parameters in constructors System.out.println("Parameters: " + c.getParameterCount()); } } catch(Exception e) { e.printStackTrace(); } } } ``` **輸出**: ```java Constructor Name: Dog Modifier: public Parameters: 0 Constructor Name: Dog Modifier: public Parameters: 1 Constructor Name: Dog Modifier: private Parameters: 2 ```
                  <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>

                              哎呀哎呀视频在线观看