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


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


java.lang.reflect.Field的getByte()方法用於獲取必須為靜態或實例字段類型的byte值。當一個類包含一個靜態或實例字節字段,而我們想獲取該字段的值時,則可以使用此方法返回Field的值。

用法:

public Byte getByte(Object obj)
            throws IllegalArgumentException,
                   IllegalAccessException

參數:此方法接受單個參數obj,它是要從中提取字節值的對象。


返回值:此方法返回轉換為字節類型的字段的值。

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

  1. IllegalAccessException:如果Field對象正在強製執行Java語言訪問控製並且基礎字段不可訪問,則拋出此異常。
  2. IllegalArgumentException:如果指定的對象不是聲明基礎字段的類或接口的實例,或者無法通過擴展轉換將字段值轉換為類型字節,則拋出此異常。
  3. NullPointerException :如果指定對象為null並且該字段是實例字段,則拋出此異常。
  4. ExceptionInInitializerError:如果此方法引發的初始化失敗,則拋出此異常。

以下示例程序旨在說明getByte()方法:
示例1:

// Java program to demonstrate the getByte() 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 identificationByte field object 
        Field field 
            = User.class
                  .getField("identificationByte"); 
  
        // Apply getByte Method on User Object 
        // to get the value of identificationByte field 
        byte value = field.getByte(user); 
  
        // print result 
        System.out.println("Value of Byte Field"
                           + " identificationByte is "
                           + value); 
  
        // Now Get the selectionByte field object 
        field = User.class.getField("selectionByte"); 
  
        // Apply getByte Method on User Object 
        // to get the value of selectionByte field 
        value = field.getByte(user); 
  
        // print result 
        System.out.println("Value of Byte Field"
                           + " selectionByte is "
                           + value); 
    } 
} 
  
// sample User class 
class User { 
  
    // static Byte values 
    public static byte identificationByte = 'E'; 
    public static byte selectionByte = 121; 
  
    // getter and setter methods 
    public static byte getIdentificationByte() 
    { 
        return identificationByte; 
    } 
  
    public static void
    setIdentificationByte(byte identificationByte) 
    { 
        User.identificationByte = identificationByte; 
    } 
  
    public static byte getSelectionByte() 
    { 
        return selectionByte; 
    } 
    public static void
    setSelectionByte(byte selectionByte) 
    { 
        User.selectionByte = selectionByte; 
    } 
}
輸出:
Value of Byte Field identificationByte is 69
Value of Byte Field selectionByte is 121

示例2:

// Java program to demonstrate the getByte() method 
  
import java.lang.reflect.Field; 
  
import java.lang.reflect.Field; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws NoSuchFieldException, 
               SecurityException, 
               IllegalArgumentException, 
               IllegalAccessException 
    { 
  
        // Create the Codes class object 
        Codes codes = new Codes(); 
  
        // Get the value field object 
        Field field 
            = Codes.class.getField("value"); 
  
        // Apply getByte Method on field Object 
        // to get the value of value field 
        byte value = field.getByte(codes); 
  
        // print result 
        System.out.println("Value: " + value); 
    } 
} 
// Codes class 
class Codes { 
    // Byte field 
    public static byte value = 12; 
}
輸出:
Value: 12

參考文獻: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getByte-java.lang.Object-



相關用法


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