Java Field getDouble()方法及示例

Java Field getDouble()方法及示例

java.lang.reflect .FieldgetDouble() 方法用于获取必须是静态或实例字段类型的double的值。这个方法也用来获取另一个原始类型的值,该类型可以通过拓扑转换为double类型。当一个类包含一个静态或实例双字段,并且我们想获得该字段的值时,我们可以使用这个方法来返回Field的值。

语法

public double getDouble(Object obj)
              throws IllegalArgumentException,
                     IllegalAccessException

参数: 该方法接受一个单参数 obj ,它是要提取双倍值的对象。

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

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

  1. IllegalAccessException: 如果Field对象正在执行Java语言的访问控制,而底层字段是不可访问的,就会抛出这个异常。
  2. IllegalArgumentException: 如果指定的对象不是声明底层字段的类或接口的实例,或者如果字段值不能通过加宽转换转换为double类型,则抛出该异常。
  3. NullPointerException: 如果指定的对象是空的,并且该字段是一个实例字段,就会抛出这个异常。
  4. ExceptionInitializerError: 如果该方法引发的初始化失败,则抛出该异常。

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

程序1 :

// 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

程序2

// 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://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getDouble-java.lang.Object-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程