如何从Java反射中获取String字段的值? [英] How to get value of String field from Java reflection?
问题描述
我有一个具有String字段的对象.我可以通过以下方式获取该字段:
I have an object that has a String field. I can obtain this field by calling:
Field field = someObj.getClass().getField("strField");
我设置了一个 Field#set(Object)
方法,用于设置此实例字段的值,但是相应的getter似乎是 Field#get(Object)
,这很奇怪,因为我希望它是 Field#get()
.
I sett a Field#set(Object)
method, for setting the value of this instance's field, but the respective getter seems to be Field#get(Object)
, which is weird because I would have expected it to be Field#get()
.
如何获取实例的 strField
的值?
推荐答案
如果您使用的是 java.lang.reflect.Field
,则设置者"为 Field.set(Object,对象)
,"getter"是 Field.get(Object)
.在两种情况下,第一个参数是您要在其上访问字段的实例.
if you are using java.lang.reflect.Field
, the "setter" is Field.set(Object,Object)
and the "getter" is Field.get(Object)
. in both cases, the first parameter is the instance on which you want to access the field.
这篇关于如何从Java反射中获取String字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!