我在使用Java中的反射来抓取类中的字段时遇到了麻烦:
public class CraftLib
{
static List alloyRecipes = new ArrayList();
public static HashSet damageOnCraft = new HashSet();
public static HashMap damageContainer = new HashMap();
public static void addAlloyResult(ur output, Object[] input)
{
alloyRecipes.add(Arrays.asList(new Object[] { input, output }));
}
//file continues
Run Code Online (Sandbox Code Playgroud)
我试着抓住这样的字段:
try {
Field[] fields = Class.forName("class.path").getFields();
for(Field f : fields) {
System.out.println(f.getName());
} catch (ClassNotFoundException e) {
System.out.println("Damn.");
}
System.out.println(fields.length);
Run Code Online (Sandbox Code Playgroud)
由于某种原因,它只能抓住damageOnCraft
和damageContainer
田地,但我真正需要的,alloyRecipes
不被抓住.我无法编辑第一个文件,那么获取和编辑该字段的最佳方法是什么?
getFields()
如果无法访问,则不会向您提供私有,受包保护或受保护的字段.
(getFields())返回一个包含Field对象的数组,该对象反映此Class对象所表示的类或接口的所有可访问公共字段
显然,您的包裹保护alloyRecipes
在您的情况下无法访问.
(getDeclaredFields())返回Field对象的数组,这些对象反映由此Class对象表示的类或接口声明的所有字段.这包括公共,受保护,默认(包)访问和私有字段,但不包括继承的字段.
归档时间: |
|
查看次数: |
696 次 |
最近记录: |