當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Field getDouble()用法及代碼示例


java.lang.reflect.Field的getDouble()方法用於獲取double值,該值必須為靜態或實例字段類型。此方法還用於獲取通過擴展轉換可轉換為double類型的另一個基本類型的值。當一個類包含一個靜態或實例雙精度字段並且我們想要獲取該字段的值時,可以使用此方法返回Field的值。

用法:

public double getDouble(Object obj)
              throws IllegalArgumentException,
                     IllegalAccessException

參數:此方法接受單個參數obj,這是從中提取雙精度值的對象。


返回值:此方法返回轉換為double類型的字段的值。

異常:此方法引發以下異常:

  1. IllegalAccessException:如果Field對象正在強製執行Java語言訪問控製並且基礎字段不可訪問,則拋出此異常。
  2. IllegalArgumentException:如果指定的對象不是聲明基礎字段的類或接口的實例,或者無法通過擴展轉換將字段值轉換為double類型,則拋出此異常。
  3. NullPointerException :如果指定對象為null並且該字段是實例字段,則拋出此異常。
  4. ExceptionInInitializerError:如果此方法引發的初始化失敗,則拋出此異常。

以下示例程序旨在說明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-



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field getDouble() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。