Java Field getChar()方法及实例

Java Field getChar()方法及实例

java.lang.reflect.FieldgetChar() 方法用于获得char的值,它必须是静态或实例字段类型。这个方法也用来获取另一个原始类型的值,该类型可以通过拓扑转换为char类型。当一个类包含一个静态或实例Char字段,并且我们想获得该字段的值时,我们可以使用这个方法来返回Field的值。

语法

public char getChar(Object obj)
       throws IllegalArgumentException,
              IllegalAccessException

参数: 该方法接受一个单参数 obj ,它是要提取char值的对象。

返回值: 该方法返回转换为char类型的字段的值。

异常: 该方法会抛出以下异常。

  1. IllegalAccessException: 如果Field对象正在执行Java语言的访问控制,而底层字段是不可访问的,就会抛出这个异常。
  2. IllegalArgumentException: 如果指定的对象不是声明底层字段的类或接口的实例,或者如果字段值不能通过拓扑转换为char类型,就会抛出这个异常。
  3. NullPointerException: 如果指定的对象是空的,并且该字段是一个实例字段,就会抛出这个异常。
  4. ExceptionInitializerError: 如果该方法引发的初始化失败,则抛出该异常。

以下程序说明了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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程