📜  Java中的字段 set() 方法及示例

📅  最后修改于: 2022-05-13 01:54:30.581000             🧑  作者: Mango

Java中的字段 set() 方法及示例

Java.lang.reflect.Fieldset()方法用于将指定对象参数上此 Field 对象表示的字段的值设置为作为参数传递的指定新值。如果基础字段具有原始类型,则新值会自动展开。如果字段是静态的,则忽略 obj 参数;它可能为 null 否则,基础字段是实例字段。

  • 此方法会根据场景抛出不同的异常,例如,如果指定的对象参数为 null,则此方法会抛出 NullPointerException
  • 如果指定的对象参数不是声明基础字段的类或接口的实例,则为 IllegalArgumentException。
  • 如果此 Field 对象正在强制执行Java语言访问控制,并且基础字段不可访问,则此方法将引发 IllegalAccessException。
  • 如果基础字段是原始类型,则此方法将引发 IllegalArgumentException,尝试展开转换以将新值转换为原始类型的值。
  • 如果此尝试失败。如果在可能的展开后,新值无法通过标识或扩展转换转换为基础字段的类型,则此方法将引发 IllegalArgumentException。
  • 如果该字段是静态的并且尚未初始化,则初始化声明该字段的类。该字段设置为可能展开和扩展的新值。如果字段隐藏在 obj 的类型中,则按照前面的规则设置字段的值。

句法:

public void set(Object obj, Object value)
         throws IllegalArgumentException,
                IllegalAccessException

参数:此方法接受两个参数:

  • obj :这是应该修改其字段的对象,并且
  • value :这是被修改的 obj 字段的新值。

Return :此方法不返回任何内容。

异常:此方法引发以下异常:

  • IllegalAccessException :如果此 Field 对象正在强制执行Java语言访问控制并且基础字段是不可访问的或最终的。
  • IllegalArgumentException :如果指定的对象不是声明基础字段(或其子类或实现者)的类或接口的实例,或者展开转换失败。
  • NullPointerException :如果指定的对象为 null 并且该字段是实例字段。
  • ExceptionInInitializerError :如果此方法引发的初始化失败。

下面的程序说明了 set() 方法:
方案一:

// Java program illustrate set() 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 uniqueNo
        System.out.println(
            "Value of uniqueNo before "
            + "applying set is "
            + emp.uniqueNo);
  
        // Get the field object
        Field field
            = Employee.class
                  .getField("uniqueNo");
  
        // Apply set Method
        field.set(emp, (short)1213);
  
        // print value of uniqueNo
        System.out.println(
            "Value of uniqueNo after "
            + "applying set is "
            + emp.uniqueNo);
  
        // print value of salary
        System.out.println(
            "Value of salary before "
            + "applying set is "
            + emp.salary);
  
        // Get the field object
        field = Employee.class.getField("salary");
  
        // Apply set Method
        field.set(emp, 324344.2323);
  
        // print value of salary
        System.out.println(
            "Value of salary after "
            + "applying set is "
            + emp.salary);
    }
}
  
// sample class
class Employee {
  
    // static values
    public static short uniqueNo = 239;
    public static double salary = 121324.13333;
}
输出:
Value of uniqueNo before applying set is 239
Value of uniqueNo after applying set is 1213
Value of salary before applying set is 121324.13333
Value of salary after applying set is 324344.2323

方案二:

// Java program illustrate set() method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws AccessException
    {
  
        // create attributes object
        attributes att = new attributes();
  
        // Get the value field object
        Field field1
            = attributes.class
                  .getField("bolValue");
        Field field2
            = attributes.class
                  .getField("intValue");
        Field field3
            = attributes.class
                  .getField("doubleValue");
  
        // Apply set Method
        field1.set(att, false);
        field2.set(att, 1213);
        field3.set(att, 342414.131);
  
        // print value of isActive
        System.out.println(
            "Values after "
            + "applying set are { "
            + att.bolValue + ", "
            + att.intValue
            + ", " + att.doubleValue
            + " }.");
    }
}
  
// sample attributes class
class attributes {
  
    // static value value
    public static boolean bolValue = false;
    public static int intValue = 13134;
    public static double doubleValue = 1314.141;
}
输出:
Values after applying set are { false, 1213, 342414.131 }

参考资料: https: Java/lang/reflect/Field.html#set-java.lang.Object-java.lang.Object-