java.lang.reflect.Field的get()方法用於獲取字段對象的值。如果Field具有原始類型,則該字段的值將自動包裝在一個對象中。如果字段是靜態字段,則obj的參數將被忽略;它可以為null,否則,基礎字段是實例字段。如果指定的obj參數為null,則此方法將引發NullPointerException;如果指定的對象不是聲明基礎字段的類或接口的實例,則此方法將引發IllegalArgumentException。如果該字段以obj類型隱藏,則該字段的值將根據前麵的規則獲得。
用法:
public double get(Object obj) throws IllegalArgumentException, IllegalAccessException
參數:此方法接受單個參數obj,它是要從中提取字段值的對象。
返回值:此方法返回對象obj中表示字段的值;基本值在返回之前被包裝在適當的對象中。
異常:此方法引發以下異常:
- IllegalAccessException–如果此Field對象正在實施Java語言訪問控製,並且基礎字段不可訪問。
- IllegalArgumentException–如果指定的對象不是聲明基礎字段(或其子類或實現者)的類或接口的實例。
- NullPointerException –如果指定的對象為null,並且該字段是實例字段。
- ExceptionInInitializerError–如果此方法引發的初始化失敗。
以下示例程序旨在說明get()方法:
示例1:
// Java program to demonstrate get() method
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws Exception
{
// Create the User class object
User user = new User();
// Get the all field objects of User class
Field[] fields = User.class.getFields();
for (int i = 0; i < fields.length; i++) {
// get value of the fields
Object value = fields[i].get(user);
// print result
System.out.println("Value of Field "
+ fields[i].getName()
+ " is " + value);
}
}
}
// sample User class
class User {
// static double values
public static double Marks = 34.13;
public static int Fees = 34199;
public static String name = "Aman";
public static double getMarks()
{
return Marks;
}
public static void setMarks(double marks)
{
Marks = marks;
}
public static int getFees()
{
return Fees;
}
public static void setFees(int fees)
{
Fees = fees;
}
public static String getName()
{
return name;
}
public static void setName(String name)
{
User.name = name;
}
}
輸出:
Value of Field Marks is 34.13 Value of Field Fees is 34199 Value of Field name is Aman
示例2:
// Java program to demonstrate get() method
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws Exception
{
// Create the RealNumbers class object
Fields field = new Fields();
// Get the all field objects of User class
Field[] fieldsOfFieldClass
= Fields.class.getFields();
for (int i = 0;
i < fieldsOfFieldClass.length; i++) {
// get value
Object value = fieldsOfFieldClass[i]
.get(field);
// print result
System.out.println(
"Value of Field "
+ fieldsOfFieldClass[i].getName()
+ " is " + value);
}
}
// RealNumbers class
static class Fields {
// double field
public static final double doubleValue
= 9999999.34567;
public static final int intValue
= 9999999;
public static final float floatValue
= 9999999.34567f;
public static final
boolean booleanValue
= true;
}
}
輸出:
Value of Field doubleValue is 9999999.34567 Value of Field intValue is 9999999 Value of Field floatValue is 9999999.0 Value of Field booleanValue is true
參考文獻: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#get-java.lang.Object-
相關用法
- Java Field set()用法及代碼示例
- Java Field getByte()用法及代碼示例
- Java Field getName()用法及代碼示例
- Java Field getGenericType()用法及代碼示例
- Java Field getAnnotation()用法及代碼示例
- Java Field getAnnotationsByType()用法及代碼示例
- Java Field toGenericString()用法及代碼示例
- Java Field getDouble()用法及代碼示例
- Java Field getFloat()用法及代碼示例
- Java Field getBoolean()用法及代碼示例
- Java Field equals()用法及代碼示例
- Java Field isSynthetic()用法及代碼示例
- Java Field getDeclaringClass()用法及代碼示例
- Java Field setShort()用法及代碼示例
- Java Field getShort()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field get() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。