簡單介紹一些類和接口的用法:
1)Member成員是一種接口,反映有關單個成員(字段或方法)或構造方法的標識信息
2)InvocationHandler是代理實例的調用處理程序實現的接口(這個接口的具體用法等到java反射機制剖析4著重介紹)
3)Method提供一個類的方法的信息以及訪問類的方法的接口。
示例:
- import java.lang.reflect.Method;
-
- public class TestMethod {
-
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- Class classType=Class.forName(args[0]);
- Method methods[]=classType.getDeclaredMethods();
- for(int i=0;i<methods.length;i++){
- System.out.println(methods[i].toString());
- }
- }
-
- }
|
4)Filed提供一個類的域的信息以及訪問類的域的接口。
示例:
- import java.lang.reflect.Field;
-
-
- public class TestField {
-
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- Class classType=Class.forName(args[0]);
- Field[] fields = classType.getFields();
- for(int i=0;i<fields.length;i++){
- System.out.println(fields[i].toString());
- }
- }
-
- }
|
5)Array類提供了動態創建和訪問Java數組的方法。
示例:
- import java.lang.reflect.Array;
-
-
- public class TestArray {
-
- public TestArray(){
-
- }
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
-
- Class<?> classType = Class.forName("java.lang.String");
-
- Object array = Array.newInstance(classType, 10);
-
- Array.set(array, 5, "hello");
-
- String s = (String)Array.get(array, 5);
- System.out.println(s);
-
- }
-
- }
|
6)Proxy提供動態地生成代理類和類實例的靜態方法(這個方法在java放射機制剖析4著重介紹)。
其余的類和接口的使用方法詳見API