當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Field setChar()用法及代碼示例


java.lang.reflect.Field的setChar()方法用於將字段的值設置為指定對象上的char。當需要將對象的字段值設置為char時,可以使用此方法在Object上設置值。

用法:

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

參數:此方法接受接受兩個參數:


  • obj:是應該修改其字段的對象,並且
  • c:這是要修改的obj字段的新值。

返回:此方法不返回任何內容。

異常:此方法引發以下異常:

  • IllegalAccessException:如果此Field對象正在強製執行Java語言訪問控製,並且基礎字段是不可訪問的或最終的。
  • IllegalArgumentException:如果指定的對象不是聲明基礎字段(或其子類或實現者)的類或接口的實例,或者展開轉換失敗。
  • NullPointerException:如果指定的對象為null,並且該字段是實例字段。
  • ExceptionInInitializerError:如果此方法引發的初始化失敗。

以下示例程序旨在說明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-



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field setChar() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。