📜  Java中的字段 getDouble() 方法及示例

📅  最后修改于: 2022-05-13 01:55:48.889000             🧑  作者: Mango

Java中的字段 getDouble() 方法及示例

Java.lang.reflect.FieldgetDouble()方法用于获取 double 的值,该值必须是静态或实例字段类型。此方法还用于获取另一个可通过扩展转换转换为 double 类型的原始类型的值。当一个类包含一个静态或实例双字段并且我们想要获取该字段的值时,我们可以使用此方法返回字段的值。

句法:

public double getDouble(Object obj)
              throws IllegalArgumentException,
                     IllegalAccessException

参数:此方法接受单个参数obj ,它是从中提取双精度值的对象。

返回值:该方法返回字段转换为double类型的值。

异常:此方法抛出以下异常:

  1. IllegalAccessException:如果 Field 对象正在执行Java语言访问控制并且基础字段不可访问,则会引发此异常。
  2. IllegalArgumentException:如果指定的对象不是声明基础字段的类或接口的实例,或者如果字段值无法通过扩大转换转换为 double 类型,则会引发此异常。
  3. NullPointerException:如果指定对象为空且字段为实例字段,则抛出此异常。
  4. ExceptionInInitializerError:如果此方法引发的初始化失败,则抛出此异常。

下面的程序说明了 getDouble() 方法:
方案一:

// Java program to demonstrate the getDouble() 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 marks field object
        Field field = User.class.getField("Marks");
  
        // Apply getDouble Method on User Object
        // to get the value of Marks field
        double value = field.getDouble(user);
  
        // print result
        System.out.println("Value of double Field"
                           + " Marks is " + value);
  
        // Now Get the Fees field object
        field = User.class.getField("Fees");
  
        // Apply getDouble Method on User Object
        // to get the value of Fees field
        value = field.getDouble(user);
  
        // print result
        System.out.println("Value of double Field"
                           + " Fees is " + value);
    }
}
  
// sample User class
class User {
  
    // static double values
    public static double Marks = 34.13;
    public static double Fees = 3413.99;
    public static String name = "Aman";
  
    public static double getMarks()
    {
        return Marks;
    }
  
    public static void setMarks(double marks)
    {
        Marks = marks;
    }
  
    public static double getFees()
    {
        return Fees;
    }
  
    public static void setFees(double fees)
    {
        Fees = fees;
    }
  
    public static String getName()
    {
        return name;
    }
  
    public static void setName(String name)
    {
        User.name = name;
    }
}
输出:
Value of double Field Marks is 34.13
Value of double Field Fees is 3413.99

方案二:

// Java program to demonstrate the getDouble() method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException,
               IllegalArgumentException,
               IllegalAccessException
    {
  
        // Create the RealNumbers class object
        RealNumbers real = new RealNumbers();
  
        // Get the value field object
        Field field
            = RealNumbers.class
                  .getField("value");
  
        // Apply getDouble Method on field Object
        // to get the value of value field
        double value = field.getDouble(real);
  
        // print result
        System.out.println("Value: " + value);
    }
  
    // RealNumbers class
    static class RealNumbers {
  
        // double field
        public static double value
            = 9999999.34567;
  
        // getter and setter methods
        public static double getValue()
        {
            return value;
        }
  
        public static void setValue(double value)
        {
            RealNumbers.value = value;
        }
    }
}
输出:
Value: 9999999.34567

参考资料: https: Java/lang/reflect/Field.html#getDouble-java.lang.Object-