java.lang.reflect.Field的getByte()方法用于获取必须为静态或实例字段类型的byte值。当一个类包含一个静态或实例字节字段,而我们想获取该字段的值时,则可以使用此方法返回Field的值。
用法:
public Byte getByte(Object obj) throws IllegalArgumentException, IllegalAccessException
参数:此方法接受单个参数obj,它是要从中提取字节值的对象。
返回值:此方法返回转换为字节类型的字段的值。
异常:此方法引发以下异常:
- IllegalAccessException:如果Field对象正在强制执行Java语言访问控制并且基础字段不可访问,则抛出此异常。
- IllegalArgumentException:如果指定的对象不是声明基础字段的类或接口的实例,或者无法通过扩展转换将字段值转换为类型字节,则抛出此异常。
- NullPointerException :如果指定对象为null并且该字段是实例字段,则抛出此异常。
- 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-
相关用法
- Java Field set()用法及代码示例
- Java Field get()用法及代码示例
- Java Field getLong()用法及代码示例
- Java Field hashCode()用法及代码示例
- Java Field getDeclaringClass()用法及代码示例
- Java Field getInt()用法及代码示例
- Java Field getName()用法及代码示例
- Java Field getShort()用法及代码示例
- Java Field getType()用法及代码示例
- Java Field equals()用法及代码示例
- Java Field getBoolean()用法及代码示例
- Java Field getChar()用法及代码示例
- Java Field getFloat()用法及代码示例
- Java Field getDouble()用法及代码示例
- Java Field setBoolean()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Field getByte() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。