Java Field setDouble()方法及实例

Java Field setDouble()方法及实例

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

语法

public void setDouble(Object obj, double d)
            throws IllegalArgumentException,
                   IllegalAccessException

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

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

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

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

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

下面的程序说明了setDouble()方法。

程序1 :

// Java program to illustrate setDouble() 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 setDouble is "
            + emp.salary);
 
        // Get the field object
        Field field = Employee.class
                          .getField("salary");
 
        // Apply setDouble Method
        field.setDouble(emp, 50000.99);
 
        // print value of salary
        System.out.println(
            "Value of salary after "
            + "applying setDouble is "
            + emp.salary);
 
        // print value of pf
        System.out.println(
            "Value of pf before "
            + "applying setDouble is "
            + emp.pf);
 
        // Get the field object
        field = Employee.class.getField("pf");
 
        // Apply setDouble Method
        field.setDouble(emp, 1234.34);
 
        // print value of pf
        System.out.println(
            "Value of pf after "
            + "applying setDouble is "
            + emp.pf);
    }
}
 
// sample class
class Employee {
 
    // static double values
    public static double pf = 2342.89;
    public static double salary = 43125;
}

输出

Value of salary before applying setDouble is 43125.0
Value of salary after applying setDouble is 50000.99
Value of pf before applying setDouble is 2342.89
Value of pf after applying setDouble is 1234.34

程序2

// Java program to illustrate setDouble() 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 setDouble Method
        field.setDouble(no, 53245.466);
 
        // print value of isActive
        System.out.println(
            "Value after "
            + "applying setDouble is "
            + Numbers.value);
    }
}
 
// sample Numbers class
class Numbers {
 
    // static double value
    public static double value = 1232.3685;
}

输出

Value after applying setDouble is 53245.466

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程