JAVA反射类

来源:互联网 发布:java免费开源网店系统 编辑:程序博客网 时间:2024/06/05 11:19

import java.lang.reflect.Array;

public class ReflectionTest {

    public static void main(String[] args) {
        try {
            Example obj = new Example();

            java.lang.reflect.Field[] F1 = obj.getClass().getDeclaredFields();
            String Name = new String();
            for (int i = 0; i < F1.length; i++) {
                System.out.println(F1[i].getType());
                System.out.println(F1[i].getName());
                Name = F1[i].getName();
            }
            //Assume that you have known that "F1[1]" is the char array
            int length = Array.getLength(F1[1].get(obj));
            System.out.println("The length of c1 is: " + length);
        } catch (Exception e) {
            System.out.println("err occur");
        }
    }
}

class Example {

    int I1 = 3;

    char c1[] = new char[10];
}

原创粉丝点击