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];
}
JAVA反射类
Java反射机制示例
最新推荐文章于 2025-12-08 09:18:56 发布
本文通过一个具体的Java程序示例介绍了如何使用Java反射API来获取类的字段信息,并演示了如何通过反射操作数组类型的字段来获取其长度。
2592

被折叠的 条评论
为什么被折叠?



