java - 用java中的反射检查一个字段是真还是假
问题描述
目前正在尝试编写一些菜单功能,并想知道是否有办法找出布尔字段是真还是假。这是我目前正在尝试的代码,但出现错误
try{
field = a.getClass().getField(b);
if(toggle==1&&field){
}else if(toggle==1&&!field){
field.set(a, true);
}else if(toggle==0&&!field){
}else if(toggle==0&&field){
field.set(a, false);
}
}catch (NullPointerException e) {
}catch (NoSuchFieldException e) {
}catch (IllegalAccessException e) {
}
错误是
The operator && is undefined for the argument type(s) boolean, Field
解决方案
您需要根据实例获取字段的值。
boolean value = field.getBoolean(instance);
推荐阅读
- c - 客户端服务器程序中的多线程概念
- sql - 错误:违反外键约束,父表中不存在键(但它是??)
- php - 我可以在 Wordpress 编辑器中将 3 个最近帖子的列表动态输出到默认文本中吗?
- vue.js - 将 v-bind 与过滤器结合使用时如何保持反应性
- java - 导航控制器方向
- javascript - 使用 JS 生成多个 html 元素时,最有效的方式是什么?
- javascript - 赛普拉斯:运行所有规范,但创建一个没有运行规范的循环
- c++ - constexpr 函数的 const 引用参数:gcc/msvc vs clang/icc
- android - 在 JSONObject 中管理 JsonObject - Kotlin
- asp.net - AWS 负载均衡器和 IIS 虚拟目录