java.lang.reflect.Field的getChar()方法用於獲取必須為靜態或實例字段類型的char值。此方法還用於通過擴展轉換獲取可轉換為char類型的另一個基本類型的值。當一個類包含一個靜態或實例Char字段並且我們想要獲取該字段的值時,我們可以使用此方法返回Field的值。
用法:
public char getChar(Object obj) throws IllegalArgumentException, IllegalAccessException
參數:此方法接受單個參數obj,這是從中提取char值的對象。
返回值:此方法返回轉換為char類型的字段的值。
異常:此方法引發以下異常:
- IllegalAccessException:如果Field對象正在強製執行Java語言訪問控製並且基礎字段不可訪問,則拋出此異常。
- IllegalArgumentException:如果指定的對象不是聲明基礎字段的類或接口的實例,或者無法通過擴展轉換將字段值轉換為char類型,則拋出此異常。
- NullPointerException :如果指定對象為null並且該字段是實例字段,則拋出此異常。
- ExceptionInInitializerError:如果此方法引發的初始化失敗,則拋出此異常。
以下示例程序旨在說明getChar()方法:
示例1:
// Java program to demonstrate the above 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 identificationChar field object
Field field
= User.class.getField("identificationChar");
// Apply getChar Method on User Object
// to get the value of identificationChar field
char value = field.getChar(user);
// print result
System.out.println("Value of Char Field"
+ " identificationChar is "
+ value);
// Now Get the selectionChar field object
field = User.class.getField("selectionChar");
// Apply getChar Method on User Object
// to get the value of selectionChar field
value = field.getChar(user);
// print result
System.out.println("Value of Char Field"
+ " selectionChar is "
+ value);
}
}
// sample User class
class User {
// static char values
public static char identificationChar = 'E';
public static char selectionChar = 'A';
public static String name = "Aman";
// getter and setter methods
public static char getIdentificationChar()
{
return identificationChar;
}
public static void
setIdentificationChar(char identificationChar)
{
User.identificationChar = identificationChar;
}
public static char getSelectionChar()
{
return selectionChar;
}
public static void
setSelectionChar(char selectionChar)
{
User.selectionChar = selectionChar;
}
public static String getName()
{
return name;
}
public static void setName(String name)
{
User.name = name;
}
}
輸出:
Value of Char Field identificationChar is E Value of Char Field selectionChar is A
示例2:
// Java program to demonstrate the above method
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws NoSuchFieldException,
SecurityException,
IllegalArgumentException,
IllegalAccessException
{
// Create the Alphabets class object
Alphabets alphabet = new Alphabets();
// Get the value field object
Field field
= Alphabets.class.getField("value");
// Apply getChar Method on field Object
// to get the value of value field
char value = field.getChar(alphabet);
// print result
System.out.println("Value: " + value);
}
// Alphabets class
static class Alphabets {
// char field
public static char value = 'Q';
// getter and setter methods
public static char getValue()
{
return value;
}
public static void setValue(char value)
{
Alphabets.value = value;
}
}
}
輸出:
Value: Q
參考文獻: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getChar-java.lang.Object-
相關用法
- Java ByteBuffer getChar()用法及代碼示例
- Java Field set()用法及代碼示例
- Java Field get()用法及代碼示例
- Java Field setInt()用法及代碼示例
- Java Field getBoolean()用法及代碼示例
- Java Field setLong()用法及代碼示例
- Java Field setByte()用法及代碼示例
- Java Field setFloat()用法及代碼示例
- Java Field getType()用法及代碼示例
- Java Field setShort()用法及代碼示例
- Java Field toGenericString()用法及代碼示例
- Java Field setDouble()用法及代碼示例
- Java Field setChar()用法及代碼示例
- Java Field equals()用法及代碼示例
- Java Field toString()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field getChar() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。