No. | 方法 | 類型 | 描述 |
1 | public static Class<?> forName(String className) throws ClassNotFoundException | 普通 | 傳入完整的“包.類”名稱實例化Class對象 |
2 | public Constructor[] getConstructors() throws SecurityException | 普通 | 得到一個類中的全部構造方法 |
3 | public Field[] getDeclaredFields() throws SecurityException | 普通 | 得到一個類父類中的全部屬性 |
4 | public Field[] getFields() throws SecurityException | 普通 | 取得本類的全部屬性 |
5 | public Method[] getMethods() throws SecurityException | 普通 | 得到一個類中的全部方法 |
6 | public Method getMethod(String name,Class... parameterTypes)throws NoSuchMethodException,SecurityException | 普通 | 返回一個Method對象,并設置一個方法中的所有參數類型 |
7 | public Class[] getInterfaces() | 普通 | 得到一個類中所實現的全部接口 |
8 | public String getName() | 普通 | 得到一個類完整的“包.類”名稱 |
9 | public Package getPackage() | 普通 | 得到一個類的包 |
10 | public Class getSuperclass() | 普通 | 得到一個類的父類 |
11 | public Object newInstance() throws InstantiationException,IllegalaccessException | 普通 | 根據Class定義的類實例化對象 |
12 | public Class<?> getComponentType() | 普通 | 返回表示數組類型的Class |
13 | public boolean isArray() | 普通 | 判斷此Class是否是一個數組 |
package com.pb.reflect;/* * 1.包 * 2.注解 * 3.構造方法 * 4.方法 * 5.內部類 */@DePRecatedpublic class ReflectDemo2 { //私有的構造方法 private ReflectDemo2(){}; //公有的帶一個一個name屬性的構造方法 public ReflectDemo2(String name){ }; //無參數的info方法 public void info(){}; //有參數的info方法 public void info(String str){}; //內部類 class inner{}; }
測試類
package demo;import java.lang.annotation.Annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import com.pb.reflect.ReflectDemo2;public class Demo3 { public static void main(String[] args) throws SecurityException, NoSuchMethodException, ClassNotFoundException { //獲取ReflectDemo2的Class對象 Class<ReflectDemo2> cla=ReflectDemo2.class; //獲取ReflectDemo2中全部構造方法 Constructor[] constructors=cla.getDeclaredConstructors(); for (Constructor con : constructors) { System.out.println(con); } //獲取指定的公有構造方法 System.out.println("====獲取指定的公有構造方法========"); Constructor [] pub=cla.getConstructors(); for (Constructor c : pub) { System.out.println(c.toString()); } //獲取公有的方法 System.out.println("====獲取公有的方法======"); Method [] methods=cla.getMethods(); for (Method meth : methods) { System.out.println(meth.toString()); } //獲取指定方法 System.out.println("====獲取指定的的方法======"); //獲取info不帶參數方法 Method method=cla.getMethod("info",null); System.out.println(method.toString()); //獲取info帶一個String參數的方法 Method meth=cla.getMethod("info", String.class); System.out.println(meth.toString()); System.out.println("=====獲取注釋======="); Annotation [] annotations=cla.getAnnotations(); for (Annotation anno : annotations) { System.out.println(anno.toString()); } //獲取得包信息 System.out.println("=====獲取得包信息======="); Package pack=cla.getPackage(); System.out.println(pack.toString()); //獲取內部類 System.out.println("=====獲取內部類======="); Class [] clas=cla.getDeclaredClasses(); for (Class c : clas) { System.out.println(c.toString()); } System.out.println("=====獲取父類======="); Class class2=cla.getSuperclass(); System.out.println(class2.toString()); //獲取內部類的對象 Class innercla=Class.forName("com.pb.reflect.ReflectDemo2$inner"); System.out.println("內部類對應的外部類:"+innercla.getDeclaringClass()); System.out.println("內部類的包:"+innercla.getPackage()); System.out.println("內部類的父類:"+innercla.getSuperclass()); }}四、創建對象
直接使用newInstance方法創建對象
package demo;import java.util.Date;public class Demo4 { public static void main(String[] args) throws InstantiationException, IllegalAccessException { //獲取Date類的Class對象 Class cla=Date.class; //使用newInstrance方法創建對象 Date date=(Date) cla.newInstance(); System.out.println(date.toString()); }}
指定的構造方法創建對象
package demo;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.util.Date;public class Demo4 { public static void main(String[] args) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { //獲取Date類的Class對象 Class cla=Date.class; //獲取指定參數的構造方法帶一個長整型參數的構造方法 Constructor constructor=cla.getConstructor(long.class); //使用newInstrance方法創建對象 Date date=(Date) constructor.newInstance(1999); System.out.println(date.toString()); }}五、調用方法
package com.pb.reflect;public class Person { private String name; private String gender; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSay(){ return this.name+" /t"+this.gender+"/t"+this.age; }}
測試類
package demo;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import com.pb.reflect.Person;public class Demo6 { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { //得到Person類的Class對象 Class cla=Person.class; //聲明一個Person對象 Person p=new Person(); //調用setName方法 Method method=cla.getMethod("setName", String.class); //使用invoke賦值 method.invoke(p, "張三"); //調用setGender方法 Method setGender=cla.getMethod("setGender", String.class); setGender.invoke(p, "女"); //調用setAge方法 Method setAge=cla.getMethod("setAge", int.class); setAge.invoke(p, 23); //調用get方法 get方法沒有參數設置為null Method getName=cla.getMethod("getName", null); Object o=getName.invoke(p, null); System.out.println(o.toString()); Method getGender=cla.getMethod("getGender", null); Object o1=getGender.invoke(p, null); System.out.println(o1.toString()); Method getAge=cla.getMethod("getAge", null); Object o2=getAge.invoke(p, null); System.out.println(o2.toString()); Method say=cla.getMethod("getSay", null); Object o3=say.invoke(p, null); System.out.println(o3.toString()); }}
結果:
張三女23張三 女 23
package com.pb.reflect;public class Person { private String name; private String gender; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSay(){ return"姓名:"+this.name+" /t性別:"+this.gender+"/t年齡:"+this.age; }}
測試類
package demo1;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Calendar;import com.pb.reflect.Person;public class Test2 { public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { //聲明Person對象 Person p=new Person(); //通過反射重到Person的Class對象 Class cla=Person.class; //因為Person類的屬性都是私有的要用getDeclaredField Field name=cla.getDeclaredField("name"); //臨時取消權限檢查 name.setAccessible(true); name.set(p, "張三豐"); //獲取年齡 Field gender=cla.getDeclaredField("gender"); //臨時取消權限檢查 gender.setAccessible(true); //賦值 gender.set(p, "男"); //獲取年齡 Field age=cla.getDeclaredField("age"); age.setAccessible(true); age.setInt(p, 108); //獲取say方法 Method getSay=cla.getMethod("getSay", null); Object o=getSay.invoke(p, null); System.out.println(o.toString()); }}
實際在在使用已知類時沒有必要使用反射,只有在程序需要動態創建某個類的對象的時候我們才會考慮使用反射
七、動態創建和訪問數組package demo1;import java.lang.reflect.Array;public class ArrayTest1 { /** * 動態創建數組和訪問數組 */ public static void main(String[] args) { //創建一個類型為String 的數組長度為10 Object arr=Array.newInstance(String.class, 10); //依次為arr賦值 Array.set(arr, 0, "一"); Array.set(arr, 1, "二"); Array.set(arr, 2, "三"); Array.set(arr, 3, "四"); Array.set(arr, 4, "五"); Array.set(arr,5, "六"); Array.set(arr, 6, "七"); Array.set(arr, 7, "八"); Array.set(arr, 8, "九"); Array.set(arr, 9, "十"); //獲取值 Object o8=Array.get(arr, 8); System.out.println("數組 中下標為8的值為"+o8); Object o2=Array.get(arr, 2); System.out.println("數組 中下標為2的值為"+o2); }}
public static void main(String[] args) { // 創建一個一維數組 Object arr1=Array.newInstance(int.class, 10); //為下標為5,和8的賦值 Array.setInt(arr1, 5, 5); Array.setInt(arr1, 8, 8); //查看相應位置的內容 System.out.println(Array.get(arr1, 5)); System.out.println(Array.get(arr1, 8)); }
創建多維數組
package demo1;import java.lang.reflect.Array;public class ArrayTest2 { /** * 使用Array類創建二維數組 */ public static void main(String[] args) { // 創建一個二維數組5,10 Object arr1=Array.newInstance(String.class, 5,10); //為二維數組賦值 //首先獲取一維的維靈敏 Object firstIndex=Array.get(arr1, 4); //4,6賦值 Array.set(firstIndex, 6,"張三"); //為3,8賦值 Object new_firstindex=Array.get(arr1, 3); Array.set(new_firstindex, 8, "李四"); //值輸出 //將arr1數組強轉為2維數組 String [][] str=(String [][])arr1; System.out.println(str[4][6]); System.out.println(str[3][8]); }}
新聞熱點
疑難解答