Java Field set()方法及实例

Java Field set()方法及实例

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

语法

public void setInt(Object obj, int i)
            throws IllegalArgumentException,
                   IllegalAccessException

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

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

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

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

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

下面的程序说明了Value()方法:

程序1 :

// Java program to illustrate setInt() 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 salary
        System.out.println(
            "Value of salary before "
            + "applying setInt is "
            + emp.salary);
 
        // Get the  field object
        Field field = Employee.class.getField("salary");
 
        // Apply setInt Method
        field.setInt(emp, 2243599);
 
        // print value of salary
        System.out.println(
            "Value of salary after "
            + "applying setInt is "
            + emp.salary);
 
        // print value of uniqueNo
        System.out.println(
            "Value of uniqueNo before "
            + "applying setInt is "
            + emp.uniqueNo);
 
        // Get the field object
        field = Employee.class.getField("uniqueNo");
 
        // Apply setInt Method
        field.setInt(emp, 123434);
 
        // print value of uniqueNo
        System.out.println(
            "Value of uniqueNo after "
            + "applying setInt is "
            + emp.uniqueNo);
    }
}
 
// sample class
class Employee {
 
    // static int values
    public static int uniqueNo = 234289;
    public static int salary = 1125213;
}

输出

Value of salary before applying setInt is 1125213
Value of salary after applying setInt is 2243599
Value of uniqueNo before applying setInt is 234289
Value of uniqueNo after applying setInt is 123434

程序2

// Java program to illustrate setInt() method
 
import java.lang.reflect.Field;
 
public class GFG {
 
    public static void main(String[] args)
        throws Exception
    {
 
        // create Numbers object
        Numbers no = new Numbers();
 
        // Get the value field object
        Field field = Numbers.class
                          .getField("value");
 
        // Apply setInt Method
        field.setInt(no, 53266);
 
        // print value of isActive
        System.out.println(
            "Value after "
            + "applying setInt is "
            + Numbers.value);
    }
}
 
// sample Numbers class
class Numbers {
 
    // static int value
    public static int value = 13685;
}

输出

Value after applying setInt is 53266

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程