有没有办法像 isPrimitive() 一样查找 Field 是否为布尔值?
有没有一种方法可以像 isPrimitive()
一样在 Java 反射中查找 Field 是否为 boolean
?
Field fieldlist[] = clazz.getDeclaredFields();
for (int i = 0; fieldlist.length & gt; i; i++) {
Field fld = fieldlist[i];
if (fld.getClass().isPrimitive()) {
fld.setInt(object, 0);
continue;
}
}
Is there a way to find if a Field is boolean
in Java reflection the same as isPrimitive()
?
Field fieldlist[] = clazz.getDeclaredFields();
for (int i = 0; fieldlist.length & gt; i; i++) {
Field fld = fieldlist[i];
if (fld.getClass().isPrimitive()) {
fld.setInt(object, 0);
continue;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
刚刚对此进行了测试,它适用于原始布尔变量。
Just tested this and it works for primitive
boolean
variables.我相信
Boolean.class.isAssignableFrom(fld.getClass())
可用于确定该字段是否为布尔值。不过,我还没有机会测试这是否适用于基元。I believe
Boolean.class.isAssignableFrom(fld.getClass())
can be used to determine if the field is a boolean. I haven't had a chance to test whether this works for primitives though.试试这个(
Try this (reference):
这适用于原语和对象:
This is for primitive and object: