java field get方法_Java Field getDouble()用法及代码示例

java.lang.reflect.Field的getDouble()方法用于获取double值,该值必须为静态或实例字段类型。此方法还用于获取通过扩展转换可转换为double类型的另一个基本类型的值。当一个类包含一个静态或实例双精度字段并且我们想要获取该字段的值时,可以使用此方法返回Field的值。

用法:

public double getDouble(Object obj)

throws IllegalArgumentException,

IllegalAccessException

参数:此方法接受单个参数obj,这是从中提取双精度值的对象。

返回值:此方法返回转换为double类型的字段的值。

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

IllegalAccessException:如果Field对象正在强制执行Java语言访问控制并且基础字段不可访问,则抛出此异常。

IllegalArgumentException:如果指定的对象不是声明基础字段的类或接口的实例,或者无法通过扩展转换将字段值转换为double类型,则抛出此异常。

NullPointerException :如果指定对象为null并且该字段是实例字段,则抛出此异常。

ExceptionInInitializerError:如果此方法引发的初始化失败,则抛出此异常。

以下示例程序旨在说明getDouble()方法:

示例1:

// Java program to demonstrate the getDouble() method

import java.lang.reflect.Field;

public class GFG {

public static void main(String[] args)

throws NoSuchFieldException,

SecurityException,

IllegalArgumentException,

IllegalAccessException

{

// Create the User class object

User user = new User();

// Get the marks field object

Field field = User.class.getField("Marks");

// Apply getDouble Method on User Object

// to get the value of Marks field

double value = field.getDouble(user);

// print result

System.out.println("Value of double Field"

+ " Marks is " + value);

// Now Get the Fees field object

field = User.class.getField("Fees");

// Apply getDouble Method on User Object

// to get the value of Fees field

value = field.getDouble(user);

// print result

System.out.println("Value of double Field"

+ " Fees is " + value);

}

}

// sample User class

class User {

// static double values

public static double Marks = 34.13;

public static double Fees = 3413.99;

public static String name = "Aman";

public static double getMarks()

{

return Marks;

}

public static void setMarks(double marks)

{

Marks = marks;

}

public static double getFees()

{

return Fees;

}

public static void setFees(double fees)

{

Fees = fees;

}

public static String getName()

{

return name;

}

public static void setName(String name)

{

User.name = name;

}

}

输出:

Value of double Field Marks is 34.13

Value of double Field Fees is 3413.99

示例2:

// Java program to demonstrate the getDouble() method

import java.lang.reflect.Field;

public class GFG {

public static void main(String[] args)

throws NoSuchFieldException,

SecurityException,

IllegalArgumentException,

IllegalAccessException

{

// Create the RealNumbers class object

RealNumbers real = new RealNumbers();

// Get the value field object

Field field

= RealNumbers.class

.getField("value");

// Apply getDouble Method on field Object

// to get the value of value field

double value = field.getDouble(real);

// print result

System.out.println("Value: " + value);

}

// RealNumbers class

static class RealNumbers {

// double field

public static double value

= 9999999.34567;

// getter and setter methods

public static double getValue()

{

return value;

}

public static void setValue(double value)

{

RealNumbers.value = value;

}

}

}

输出:

Value: 9999999.34567

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Java GUI和链接数据库的学生信息管理系统代码: ``` import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.ArrayList; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class StudentManagementSystem extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JLabel nameLabel, idLabel, gradeLabel; private JTextField nameField, idField, gradeField; private JButton addButton, removeButton, displayButton; private JTable table; private DefaultTableModel model; private JScrollPane scrollPane; private Connection conn; private Statement stmt; public StudentManagementSystem() { super("Student Management System"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); nameLabel = new JLabel("Name:"); idLabel = new JLabel("ID:"); gradeLabel = new JLabel("Grade:"); nameField = new JTextField(10); idField = new JTextField(10); gradeField = new JTextField(10); addButton = new JButton("Add"); removeButton = new JButton("Remove"); displayButton = new JButton("Display"); addButton.addActionListener(this); removeButton.addActionListener(this); displayButton.addActionListener(this); JPanel inputPanel = new JPanel(new GridLayout(3, 2)); inputPanel.add(nameLabel); inputPanel.add(nameField); inputPanel.add(idLabel); inputPanel.add(idField); inputPanel.add(gradeLabel); inputPanel.add(gradeField); JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(addButton); buttonPanel.add(removeButton); buttonPanel.add(displayButton); model = new DefaultTableModel(new String[]{"Name", "ID", "Grade"}, 0); table = new JTable(model); scrollPane = new JScrollPane(table); add(inputPanel, BorderLayout.NORTH); add(scrollPane, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); pack(); setLocationRelativeTo(null); try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", ""); stmt = conn.createStatement(); } catch (Exception e) { e.printStackTrace(); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == addButton) { String name = nameField.getText(); String id = idField.getText(); double grade = Double.parseDouble(gradeField.getText()); try { String sql = "INSERT INTO students (name, id, grade) VALUES ('" + name + "', '" + id + "', " + grade + ")"; stmt.executeUpdate(sql); model.addRow(new Object[]{name, id, grade}); } catch (Exception ex) { ex.printStackTrace(); } nameField.setText(""); idField.setText(""); gradeField.setText(""); } else if (e.getSource() == removeButton) { int row = table.getSelectedRow(); String id = (String) model.getValueAt(row, 1); try { String sql = "DELETE FROM students WHERE id='" + id + "'"; stmt.executeUpdate(sql); model.removeRow(row); } catch (Exception ex) { ex.printStackTrace(); } } else if (e.getSource() == displayButton) { try { String sql = "SELECT * FROM students"; ResultSet rs = stmt.executeQuery(sql); model.setRowCount(0); while (rs.next()) { String name = rs.getString("name"); String id = rs.getString("id"); double grade = rs.getDouble("grade"); model.addRow(new Object[]{name, id, grade}); } } catch (Exception ex) { ex.printStackTrace(); } } } public static void main(String[] args) { new StudentManagementSystem().setVisible(true); } } ``` 这个学生信息管理系统使用了 Java Swing GUI 组件来创建用户界面,并链接了 MySQL 数据库来存储和管理学生信息。它提供了添加、删除和显示学生信息的功能。在 actionPerformed 方法中,根据用户操作的不同,使用 SQL 语句来操作数据库,并更新表格数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值