java.lang.reflect.Field的getDouble()方法用于获取double值,该值必须为静态或实例字段类型。此方法还用于获取通过扩展转换可转换为double类型的另一个基本类型的值。当一个类包含一个静态或实例双精度字段并且我们想要获取该字段的值时,可以使用此方法返回Field的值。
用法:
public double getDouble(Object obj) throws IllegalArgumentException, IllegalAccessException
参数:此方法接受单个参数obj,这是从中提取双精度值的对象。
返回值:此方法返回转换为double类型的字段的值。
异常:此方法引发以下异常:
- IllegalAccessException:如果Field对象正在强制执行Java语言访问控制并且基础字段不可访问,则抛出此异常。
- IllegalArgumentException:如果指定的对象不是声明基础字段的类或接口的实例,或者无法通过扩展转换将字段值转换为double类型,则抛出此异常。
- NullPointerException :如果指定对象为null并且该字段是实例字段,则抛出此异常。
- 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-
相关用法
- Java ByteBuffer getDouble()用法及代码示例
- Java Array getDouble()用法及代码示例
- Java Field get()用法及代码示例
- Java Field set()用法及代码示例
- Java Field setShort()用法及代码示例
- Java Field setByte()用法及代码示例
- Java Field isSynthetic()用法及代码示例
- Java Field setLong()用法及代码示例
- Java Field setBoolean()用法及代码示例
- Java Field toGenericString()用法及代码示例
- Java Field getDeclaredAnnotations()用法及代码示例
- Java Field getAnnotationsByType()用法及代码示例
- Java Field getFloat()用法及代码示例
- Java Field getByte()用法及代码示例
- Java Field getAnnotation()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Field getDouble() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。