Java Field setChar()方法及实例

Java Field setChar()方法及实例

java.lang.reflect.FieldsetChar() 方法用于将一个字段的值设置为指定对象上的char。当你需要将一个对象的一个字段的值设置为char时,你可以使用这个方法来设置一个对象的值。

语法

public void setChar(Object obj, char c)
            throws IllegalArgumentException,
                   IllegalAccessException

参数: 该方法接受两个参数。

  • obj : 对象,其字段应被修改,和
  • c :被修改的obj字段的新值。

返回 :该方法不返回任何东西。

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

  • IllegalAccessException:如果这个字段对象正在执行Java语言的访问控制,并且底层字段是不可访问的或最终的。
  • IllegalArgumentException:如果指定的对象不是声明底层字段的类或接口的实例(或其子类或实现者),或者解包转换失败。
  • NullPointerException:如果指定的对象是空的,并且该字段是一个实例字段。
  • ExceptionInitializerError:如果这个方法引发的初始化失败。

下面的程序说明了setChar()方法。

程序1 :

// Java program to illustrate setByte() method
 
// program illustrate setChar()
import java.lang.reflect.Field;
 
public class GFG {
 
    public static void main(String[] args)
        throws Exception
    {
 
        // create user object
        Employee emp = new Employee();
 
        // print value of lastNamePrefix
        System.out.println(
            "Value of lastNamePrefix before "
            + "applying setChar is "
            + emp.lastNamePrefix);
 
        // Get the marks field object
        Field field = Employee.class
                          .getField("lastNamePrefix");
 
        // Apply setChar Method
        field.setChar(emp, 'B');
 
        // print value of lastNamePrefix
        System.out.println(
            "Value of lastNamePrefix after "
            + "applying setChar is "
            + emp.lastNamePrefix);
 
        // print value of firstNamePrefix
        System.out.println(
            "Value of firstNamePrefix before "
            + "applying setChar is "
            + emp.firstNamePrefix);
 
        // Get the marks field object
        field = Employee.class
                    .getField("firstNamePrefix");
 
        // Apply setChar Method
        field.setChar(emp, 'Z');
 
        // print value of firstNamePrefix
        System.out.println(
            "Value of firstNamePrefix after "
            + "applying setChar is "
            + emp.firstNamePrefix);
    }
}
 
// sample class
class Employee {
 
    // static char values
    public static char firstNamePrefix = 'A';
    public static char lastNamePrefix = 'S';
}

输出

Value of lastNamePrefix before applying setChar is S
Value of lastNamePrefix after applying setChar is B
Value of firstNamePrefix before applying setChar is A
Value of firstNamePrefix after applying setChar is Z

程序2

// Java program to illustrate setChar() method
 
import java.lang.reflect.Field;
 
public class GFG {
 
    public static void main(String[] args)
        throws Exception
    {
 
        // create user object
        User user = new User();
 
        // Get the id field object
        Field field = User.class
                          .getField("id");
 
        // Apply setChar Method
        field.setChar(user, 'A');
 
        // print value of isActive
        System.out.println("Value after "
                           + "applying setChar is "
                           + user.id);
    }
}
 
// sample User class
class User {
 
    // static char values
    public static char id = 'Z';
}

输出

Value after applying setChar is A

参考资料: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#setChar-java.lang.Object-char-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程