Java Field setByte()方法及示例

Java Field setByte()方法及示例

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

语法

public void setByte(Object obj, byte b)
            throws IllegalArgumentException,
                   IllegalAccessException

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

  • obj : 它是一个对象,其字段应该被修改。
  • b : 被修改的obj字段的新值。

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

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

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

以下程序说明了setByte()方法:

程序1 :

// Java program to illustrate setByte() method
 
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 leaveLeft
        System.out.println(
            "Value of leaveLeft before "
            + "applying setByte is "
            + emp.leaveLeft);
 
        // Get the marks field object
        Field field
            = Employee.class
                  .getField("leaveLeft");
 
        // Apply setByte Method
        field.setByte(emp, (byte)2);
 
        // print value of leaveLeft
        System.out.println(
            "Value of leaveLeft after "
            + "applying setByte is "
            + emp.leaveLeft);
 
        // print value of age
        System.out.println(
            "Value of age before "
            + "applying setByte is "
            + emp.age);
 
        // Get the marks field object
        field = Employee.class.getField("age");
 
        // Apply setByte Method
        field.setByte(emp, (byte)56);
 
        // print value of age
        System.out.println(
            "Value of age after "
            + "applying setByte is "
            + emp.age);
    }
}
 
// sample class
class Employee {
 
    // static byte values
    public static byte age = 45;
    public static byte leaveLeft = 12;
}

输出

Value of leaveLeft before applying setByte is 12
Value of leaveLeft after applying setByte is 2
Value of age before applying setByte is 45
Value of age after applying setByte is 56

程序2

// Java program to illustrate setByte() 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 setByte Method
        field.setByte(user, (byte)20);
 
        // print value of isActive
        System.out.println(
            "Value after "
            + "applying setByte is "
            + user.id);
    }
}
 
// sample User class
class User {
 
    // static byte values
    public static byte id = 2;
}

输出

Value after applying setByte is 20

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程