反射:通过反射加载的类中的常量变量 [英] Reflection: Constant variables within a class loaded via reflection
本文介绍了反射:通过反射加载的类中的常量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含一堆常量字符串的类.
I have a class which has a bunch of Constant Strings.
我需要通过反射加载此类并检索那些常量.我可以起床:
I need to load this class via reflection and retrieve those constants. I can get up to:
controllerClass = Class.forName(constantsClassName);
Object someclass = controllerClass.newInstance();
但是我对如何检索此类中的字段感到困惑.
but I am confused on how to retrieve the fields in this class.
推荐答案
访问字段的快速示例-
Field[] fields = controllerClass.getDeclaredFields();
for ( Field field : fields ) {
field.setAccessible(true);
System.out.println(field.get(someClass));
}
这篇关于反射:通过反射加载的类中的常量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文