java.lang.reflect.Field的getFloat()方法用於獲取必須為靜態或實例字段類型的float值。此方法還用於獲取通過擴展轉換可轉換為float類型的另一個原始類型的值。當類包含靜態或實例浮點型字段並且我們想要獲取該字段的值時,可以使用此方法返回Field的值。
用法:
public float getFloat(Object obj) throws IllegalArgumentException, IllegalAccessException
參數:此方法接受單個參數obj,它是要從中提取浮點值的對象。
返回值:此方法返回轉換為float類型的字段的值。
異常:此方法引發以下異常:
- IllegalAccessException:如果Field對象正在強製執行Java語言訪問控製並且基礎字段不可訪問,則拋出此異常。
- IllegalArgumentException:如果指定的對象不是聲明基礎字段的類或接口的實例,或者如果不能通過擴展轉換將字段值轉換為float類型,則拋出此異常。
- NullPointerException :如果指定對象為null並且該字段是實例字段,則拋出此異常。
- ExceptionInInitializerError:如果此方法引發的初始化失敗,則拋出此異常。
以下示例程序旨在說明getFloat()方法:
示例1:
// Java program to demonstrate the getFloat() 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 getFloat Method on User Object
// to get the value of Marks field
float value = field.getFloat(user);
// print result
System.out.println("Value of float Field"
+ " Marks is " + value);
// Now Get the Fees field object
field = User.class.getField("Fees");
// Apply getFloat Method on User Object
// to get the value of Fees field
value = field.getFloat(user);
// print result
System.out.println("Value of float Field"
+ " Fees is " + value);
}
}
// sample User class
class User {
// static float values
public static float Marks = 94.13f;
public static float Fees = 1343413.99f;
public static String name = "Aman";
public static float getMarks()
{
return Marks;
}
public static void setMarks(float marks)
{
Marks = marks;
}
public static float getFees()
{
return Fees;
}
public static void setFees(float fees)
{
Fees = fees;
}
public static String getName()
{
return name;
}
public static void setName(String name)
{
User.name = name;
}
}
輸出:
Value of float Field Marks is 94.13 Value of float Field Fees is 1343414.0
示例2:
// Java program to demonstrate the getFloat() 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 getFloat Method on field Object
// to get the value of value field
float value = field.getFloat(real);
// print result
System.out.println("Value: " + value);
}
// RealNumbers class
static class RealNumbers {
// float field
public static float value
= 123432123.1234f;
// getter and setter methods
public static float getValue()
{
return value;
}
public static void setValue(float value)
{
RealNumbers.value = value;
}
}
}
輸出:
Value: 1.2343212E8
參考文獻: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getFloat-java.lang.Object-
相關用法
- Java ByteBuffer getFloat()用法及代碼示例
- Java Array getFloat()用法及代碼示例
- Java Field set()用法及代碼示例
- Java Field get()用法及代碼示例
- Java Field getByte()用法及代碼示例
- Java Field getDeclaredAnnotations()用法及代碼示例
- Java Field getDeclaringClass()用法及代碼示例
- Java Field getAnnotation()用法及代碼示例
- Java Field getDouble()用法及代碼示例
- Java Field setShort()用法及代碼示例
- Java Field getChar()用法及代碼示例
- Java Field getGenericType()用法及代碼示例
- Java Field setBoolean()用法及代碼示例
- Java Field setDouble()用法及代碼示例
- Java Field setChar()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field getFloat() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。