当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Field get()用法及代码示例


java.lang.reflect.Field的get()方法用于获取字段对象的值。如果Field具有原始类型,则该字段的值将自动包装在一个对象中。如果字段是静态字段,则obj的参数将被忽略;它可以为null,否则,基础字段是实例字段。如果指定的obj参数为null,则此方法将引发NullPointerException;如果指定的对象不是声明基础字段的类或接口的实例,则此方法将引发IllegalArgumentException。如果该字段以obj类型隐藏,则该字段的值将根据前面的规则获得。

用法:

public double get(Object obj)
             throws IllegalArgumentException,
                    IllegalAccessException

参数:此方法接受单个参数obj,它是要从中提取字段值的对象。


返回值:此方法返回对象obj中表示字段的值;基本值在返回之前被包装在适当的对象中。

异常:此方法引发以下异常:

  1. IllegalAccessException–如果此Field对象正在实施Java语言访问控制,并且基础字段不可访问。
  2. IllegalArgumentException–如果指定的对象不是声明基础字段(或其子类或实现者)的类或接口的实例。
  3. NullPointerException –如果指定的对象为null,并且该字段是实例字段。
  4. 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-



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Field get() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。