Java Field setBoolean()方法及实例

Java Field setBoolean()方法及实例

java.lang.reflect.FieldsetBoolean() 方法用于将一个字段的值设置为指定对象上的布尔值。当你需要将一个对象的一个字段的值设置为布尔值时,你可以使用这个方法来设置一个对象的值。
语法

public void setBoolean(Object obj, boolean z)
            throws IllegalArgumentException,
                   IllegalAccessException

参数: 该方法接受两个参数。

  • obj : 对象,其字段应被修改,和
  • z : 被修改的obj字段的新值。

返回值 :该方法不返回任何东西。

异常: 该方法抛出以下异常。

  • IllegalAccessException :如果这个字段对象正在执行Java语言的访问控制,并且底层字段是不可访问的或最终的。
  • IllegalArgumentException :如果指定的对象不是声明底层字段的类或接口的实例(或其子类或实现者),或者如果解包转换失败。
  • NullPointerException : 如果指定的对象是空的,并且该字段是一个实例字段。
  • ExceptionInitializerError :如果该方法引发的初始化失败。

以下程序说明了setBoolean()方法:

程序1 :

// Java program to illustrate setBoolean() method
 
import java.lang.reflect.Field;
 
public class GFG {
 
    public static void main(String[] args)
        throws Exception
    {
 
        // create user object
        User user = new User();
 
        // print value of isActive
        System.out.println("Value before "
                           + "applying setBoolean is "
                           + user.isActive);
 
        // Get the marks field object
        Field field
            = User.class
                  .getField("isActive");
 
        // Apply setBoolean Method
        field.setBoolean(field, false);
 
        // print result
        System.out.println("Value after "
                           + "applying setBoolean is "
                           + user.isActive);
    }
}
 
// sample User class
class User {
 
    // static boolean values
    public static boolean isActive = true;
}

输出

Value before applying setBoolean is true
Value after applying setBoolean is false

程序2

// Java program to illustrate setBoolean() method
 
import java.lang.reflect.Field;
 
public class GFG {
 
    public static void main(String[] args)
        throws Exception
    {
 
        // create user object
        Employee emp = new Employee();
 
        // print value of isManager
        System.out.println("Value of isManager before "
                           + "applying setBoolean is "
                           + emp.isManager);
 
        // Get the marks field object
        Field field
            = Employee.class
                  .getField("isManager");
 
        // Apply setBoolean Method
        field.setBoolean(emp, false);
 
        // print value of isActive
        System.out.println("Value of isPresent before "
                           + "applying setBoolean is "
                           + emp.isManager);
 
        // print value of isManager
        System.out.println("Value of isManager before "
                           + "applying setBoolean is "
                           + emp.isPresent);
 
        // Get the marks field object
        field = Employee.class
                    .getField("isPresent");
 
        // Apply setBoolean Method
        field.setBoolean(emp, true);
 
        // print value of isActive
        System.out.println("Value of isPresent before "
                           + "applying setBoolean is "
                           + emp.isPresent);
    }
}
 
// sample User class
class Employee {
 
    // static boolean values
    public static boolean isPresent = false;
    public static boolean isManager = true;
}

输出

Value of isManager before applying setBoolean is true
Value of isPresent before applying setBoolean is false
Value of isManager before applying setBoolean is false
Value of isPresent before applying setBoolean is true

参考文献 : https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#setBoolean-java.lang.Object-boolean-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程