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

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

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

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

句法:

public void setFloat(Object obj, float f)
            throws IllegalArgumentException,
                   IllegalAccessException

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

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

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

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

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

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

// Java program to illustrate setFloat() 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 setFloat is "
            + emp.salary);
  
        // Get the marks field object
        Field field = Employee.class.getField("salary");
  
        // Apply setFloat Method
        field.setFloat(emp, 259939.99f);
  
        // print value of salary
        System.out.println(
            "Value of salary after "
            + "applying setFloat is "
            + emp.salary);
  
        // print value of pf
        System.out.println(
            "Value of pf before "
            + "applying setFloat is "
            + emp.pf);
  
        // Get the marks field object
        field = Employee.class.getField("pf");
  
        // Apply setFloat Method
        field.setFloat(emp, 234234.34f);
  
        // print value of pf
        System.out.println(
            "Value of pf after "
            + "applying setFloat is "
            + emp.pf);
    }
}
  
// sample class
class Employee {
  
    // static float values
    public static float pf = 122342.89f;
    public static float salary = 143125f;
}
输出:
Value of salary before applying setFloat is 143125.0
Value of salary after applying setFloat is 259939.98
Value of pf before applying setFloat is 122342.89
Value of pf after applying setFloat is 234234.34

方案二:

// Java program to illustrate setFloat() 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 setFloat Method
        field.setFloat(no, 3244.466f);
  
        // print value of isActive
        System.out.println(
            "Value after "
            + "applying setFloat is "
            + Numbers.value);
    }
}
  
// sample Numbers class
class Numbers {
  
    // static float value
    public static float value = 26774.3685f;
}
输出:
Value after applying setFloat is 3244.466

参考: https: Java/lang/reflect/Field.html#setFloat-java.lang.Object-float-