java.lang.reflect.Field類的getBoolean()方法用於獲取類中包含的靜態或實例布爾字段的值。當一個類包含一個靜態或實例布爾字段,而我們想獲取該字段的值時,則可以使用此方法返回Field的值。
用法:
public boolean getBoolean(Object obj) throws IllegalArgumentException, IllegalAccessException
參數:此方法接受單個參數obj,該對象是要從中提取布爾值的對象。
返回值:此方法返回必需的布爾字段的值。
異常:此方法引發以下異常:
- IllegalAccessException:如果Field對象正在強製執行Java語言訪問控製並且基礎字段不可訪問,則拋出此異常。
- IllegalArgumentException:如果指定的對象不是聲明基礎字段的類或接口的實例,或者如果不能通過擴展轉換將字段值轉換為布爾類型,則拋出此異常。
- NullPointerException :如果指定對象為null並且該字段是實例字段,則拋出此異常。
- ExceptionInInitializerError:如果此方法引發的初始化失敗,則拋出此異常。
以下示例程序旨在說明getBoolean()方法:
示例1:
// Java program to demonstrate the above method
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws NoSuchFieldException,
SecurityException,
IllegalArgumentException,
IllegalAccessException
{
// Create the User class object
User user = new User();
// Get the isEmployee field object
Field field = User.class.getField("isEmployee");
// Apply getBoolean Method on User Object
// to get the value of isEmployee field
boolean value = field.getBoolean(user);
// print result
System.out.println("Value of Boolean Field"
+ " isEmployee is " + value);
// Now Get the isActive field object
field = User.class.getField("isActive");
// Apply getBoolean Method on User Object
// to get the value of isActive field
value = field.getBoolean(user);
// print result
System.out.println("Value of Boolean Field"
+ " isActive is " + value);
}
}
// sample User class
class User {
// static boolean values
public static boolean isEmployee = true;
public static boolean isActive = false;
public static boolean isEmployee()
{
return isEmployee;
}
public static void setEmployee(boolean isEmployee)
{
User.isEmployee = isEmployee;
}
public static boolean isActive()
{
return isActive;
}
public static void setActive(boolean isActive)
{
User.isActive = isActive;
}
}
輸出:
Value of Boolean Field isEmployee is true
Value of Boolean Field isActive is false
示例2:
// Java program to demonstrate the above method
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws NoSuchFieldException,
SecurityException,
IllegalArgumentException,
IllegalAccessException
{
// Create the mathDigit class object
mathDigit math = new mathDigit();
// Get the isEmployee field object
Field field = mathDigit.class.getField("isZero");
// Apply getBoolean Method on field Object
// to get the value of isZero field
boolean value = field.getBoolean(math);
// print result
System.out.println("Value: " + value);
}
// mathDigit class
static class mathDigit {
public static boolean isZero = false;
public int number;
public static boolean isZero()
{
return isZero;
}
public static void setZero(boolean isZero)
{
mathDigit.isZero = isZero;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
}
}
輸出:
Value: false
參考文獻: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Field.html#getBoolean(java.lang.Object)
相關用法
- Java Array getBoolean()用法及代碼示例
- Java Field get()用法及代碼示例
- Java Field set()用法及代碼示例
- Java Field getFloat()用法及代碼示例
- Java Field getDouble()用法及代碼示例
- Java Field getName()用法及代碼示例
- Java Field getInt()用法及代碼示例
- Java Field getByte()用法及代碼示例
- Java Field equals()用法及代碼示例
- Java Field getDeclaringClass()用法及代碼示例
- Java Field setShort()用法及代碼示例
- Java Field toGenericString()用法及代碼示例
- Java Field getShort()用法及代碼示例
- Java Field getChar()用法及代碼示例
- Java Field getGenericType()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field getBoolean() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。