Java Class getDeclaredFields() 方法

2025 年 3 月 21 日 | 阅读 2 分钟

java.lang.Class 类的 getDeclaredFields() 方法返回一个 Field 对象数组,该数组表示此 Class 对象中定义的所有字段。如果没有声明字段,则返回长度为 0 的数组。

语法

参数

不适用

返回值

字段对象的数组。

抛出

安全异常 (Security Exception)

示例 1

输出

Field (1)long ClassgetDeclaredFieldsExample1.l
Field (2)int ClassgetDeclaredFieldsExample1.i
Field (3)java.lang.String ClassgetDeclaredFieldsExample1.p

示例 2

输出

Field (1)long ClassgetDeclaredFieldsExample2.l
Field (2)int ClassgetDeclaredFieldsExample2.i
Field (3)java.lang.String ClassgetDeclaredFieldsExample2.p
Field (4)float ClassgetDeclaredFieldsExample2.f
Field (5)boolean ClassgetDeclaredFieldsExample2.b
 
下一个主题Java类