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

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

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

Java.lang.reflect.FieldsetLong()方法用于在指定对象上将字段的值设置为 long。当您需要设置对象字段的值时,您可以使用此方法设置对象的值。

句法:

public void setLong(Object obj, long l)
            throws IllegalArgumentException,
                   IllegalAccessException

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

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

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

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

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

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

// Java program to illustrate setLong() 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.prlongln(
            "Value of salary before "
            + "applying setLong is "
            + emp.salary);
  
        // Get the  field object
        Field field = Employee.class.getField("salary");
  
        // Apply setLong Method
        field.setLong(emp, 10000000);
  
        // print value of salary
        System.out.prlongln(
            "Value of salary after "
            + "applying setLong is "
            + emp.salary);
  
        // prlong value of uniqueNo
        System.out.prlongln(
            "Value of uniqueNo before "
            + "applying setLong is "
            + emp.uniqueNo);
  
        // Get the field object
        field = Employee.class.getField("uniqueNo");
  
        // Apply setLong Method
        field.setLong(emp, 200000);
  
        // prlong value of uniqueNo
        System.out.prlongln(
            "Value of uniqueNo after "
            + "applying setLong is "
            + emp.uniqueNo);
    }
}
  
// sample class
class Employee {
  
    // static long values
    public static long uniqueNo = 234289;
    public static long salary = 1125213;
}
输出:
Value of salary before applying setLong is 1234567
Value of salary after applying setLong is 10000000
Value of uniqueNo before applying setLong is 234289
Value of uniqueNo after applying setLong is 2000000

方案二:

// Java program to illustrate setLong() 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 setLong Method
        field.setLong(no, 36283435);
  
        // prlong value of isActive
        System.out.prlongln(
            "Value after "
            + "applying setLong is "
            + Numbers.value);
    }
}
  
// sample Numbers class
class Numbers {
  
    // static long value
    public static long value = 121314454;
}
输出:
Value after applying setLong is 36283435

参考: https: Java/lang/reflect/Field.html#setLong-java.lang.Object-long-