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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。