公司外文网站制作,河南住房和城乡建设厅网站,搜索seo引擎,有人做几个蝎子养殖门户网站java.lang.reflect.Field的setBoolean()方法用于将字段的值设置为指定对象上的布尔值。当需要将对象的字段的值设置为布尔值时#xff0c;可以使用此方法在对象上设置值。用法:public void setBoolean(Object obj, boolean z)throws IllegalArgumentException,IllegalAccessEx…java.lang.reflect.Field的setBoolean()方法用于将字段的值设置为指定对象上的布尔值。当需要将对象的字段的值设置为布尔值时可以使用此方法在对象上设置值。用法:public void setBoolean(Object obj, boolean z)throws IllegalArgumentException,IllegalAccessException参数此方法接受两个参数obj是应该修改其字段的对象并且z这是要修改的obj字段的新值。返回值此方法不返回任何内容。异常此方法引发以下异常IllegalAccessException如果此Field对象正在实施Java语言访问控制并且基础字段是不可访问的或最终的。IllegalArgumentException如果指定的对象不是声明基础字段(或其子类或实现者)的类或接口的实例或者展开的转换失败。NullPointerException如果指定的对象为null并且该字段是实例字段。ExceptionInInitializerError如果此方法引发的初始化失败。以下示例程序旨在说明setBoolean()方法示例1:// Java program to illustrate setBoolean() methodimport java.lang.reflect.Field;public class GFG {public static void main(String[] args)throws Exception{// create user objectUser user new User();// print value of isActiveSystem.out.println(Value before applying setBoolean is user.isActive);// Get the marks field objectField field User.class.getField(isActive);// Apply setBoolean Methodfield.setBoolean(field, false);// print resultSystem.out.println(Value after applying setBoolean is user.isActive);}}// sample User classclass User {// static boolean valuespublic static boolean isActive true;}输出Value before applying setBoolean is trueValue after applying setBoolean is false示例2:// Java program to illustrate setBoolean() methodimport java.lang.reflect.Field;public class GFG {public static void main(String[] args)throws Exception{// create user objectEmployee emp new Employee();// print value of isManagerSystem.out.println(Value of isManager before applying setBoolean is emp.isManager);// Get the marks field objectField field Employee.class.getField(isManager);// Apply setBoolean Methodfield.setBoolean(emp, false);// print value of isActiveSystem.out.println(Value of isPresent before applying setBoolean is emp.isManager);// print value of isManagerSystem.out.println(Value of isManager before applying setBoolean is emp.isPresent);// Get the marks field objectfield Employee.class.getField(isPresent);// Apply setBoolean Methodfield.setBoolean(emp, true);// print value of isActiveSystem.out.println(Value of isPresent before applying setBoolean is emp.isPresent);}}// sample User classclass Employee {// static boolean valuespublic static boolean isPresent false;public static boolean isManager true;}输出Value of isManager before applying setBoolean is trueValue of isPresent before applying setBoolean is falseValue of isManager before applying setBoolean is falseValue of isPresent before applying setBoolean is true