java Class getDeclaredFields() 与getFields()的区别 博客分类: Java WebJava getDeclaredFieldsgetFields私有字段Class反射
程序员文章站
2024-02-24 19:54:52
...
java 中getDeclaredFields() 与getFields() 的区别
getDeclaredFields()返回Class中所有的字段,包括私有字段。例证:
package com.test.bean; import java.sql.Timestamp; public class Person2 { private int id; private int age; private String personName; private Timestamp birthdate; public String identitify; protected String address; String phone; } @Test public void test_getDeclaredFields() { Field[]fields=Person2.class.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; System.out.println(field.getName()); } }
运行结果:
id
age
personName
birthdate
identitify
address
phone
getFields 只返回公共字段,即有public修饰的字段。例证:
@Test public void test_getDeclaredFields() { Field[]fields=Person2.class.getFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; System.out.println(field.getName()); } }
运行结果如下:
identitify
总结:
(1)getDeclaredFields()返回Class中所有的字段,包括私有字段;
(2)getFields 只返回公共字段,即有public修饰的字段
上一篇: Java反射机制的精髓讲解