Java getDeclaredField()方法的结果是什么?
下面代码中getDeclaredField方法的功能是什么? 代码中没有定义tempClazz。有人能帮助理解下面的代码吗Java getDeclaredField()方法的结果是什么?,java,Java,下面代码中getDeclaredField方法的功能是什么? 代码中没有定义tempClazz。有人能帮助理解下面的代码吗 private static Field getDeclaredField(Class tempClazz, String fieldName) { Field field = null; try { field = tempClazz.getDeclaredField(fieldName); field.setAccessi
private static Field getDeclaredField(Class tempClazz, String fieldName) {
Field field = null;
try {
field = tempClazz.getDeclaredField(fieldName);
field.setAccessible(true);
} catch (SecurityException e) {
return field;
} catch (NoSuchFieldException e) {
tempClazz = tempClazz.getSuperclass();
if (tempClazz == null) {
throw new RuntimeException(e);
}
field = getDeclaredField(tempClazz, fieldName);
return field;
}
return field;
}
当然是:
private static Field getDeclaredField(Class tempClazz
这是该方法的一个参数
对于该方法的含义:只需研究其:
返回一个Field对象,该对象反映由该类对象表示的类或接口的指定声明字段。name参数是一个字符串,用于指定所需字段的简单名称
嗯,
tempClazz
是一个参数,您需要将一个参数传递给该方法。我们真的不清楚您在问什么……该方法实际上应该抛出一个NoSuchFieldException
(更准确地说,是反向的)。像这种方法那样包装它并没有多大意义,这种异常消息的方式会让人感到困惑,因为它基本上总是说“java.lang.Object上没有这样的字段”,而不说您实际查询的类型。