Java的.lang.reflect.Field类在Java中
软件分析自身的能力被称为反射。这是由Java.lang.reflect 包和 Class .Field 中的元素提供的,其作用与整个反射机制相同,在运行时而不是在编译时动态地分析软件组件并描述其功能。与许多其他语言一样, Java是静态类型的。反射机制允许人们在某种程度上绕过它,并引入一些更动态的特性,比如,通过名称检索字段的值。包Java.lang.reflect 包括几个接口。特别感兴趣的是 Member,它定义了允许获取有关字段、构造函数或类方法的信息的方法。这个包中还有十个类,即AccessibleObject、Array、Constructor、Executable、Field、Method、Modifier、Parameter、Proxy、ReflectPermission。
以下应用程序说明了Java反射的简单使用。它打印Java.awt.Dimension 类的字段。程序从 Class 的 forName() 方法开始,获取Java.awt.Dimension 的类对象。获得此信息后,将使用 getFields() 来分析类对象。它们返回提供有关该对象的信息的 Field 对象数组。Method Description equals (Object obj ) This method compares this field against the specified object. getAnnotatedType () This method returns an AnnotatedType object that represents the use of a type to specify the declared type of the field represented by this Field getAnnotation() This method returns this element’s annotation for the specified type if such an annotation is present, else null. getAnnotationByType() This method returns annotations that are associated with this element getBoolean(Object obj ) This method gets the value of a static or instance boolean field getByte(Object obj ) This method gets the value of a static or instance byte field getChar(Object obj ) This method gets the value of a static or instance field of type char or of another primitive type convertible to type char via a widening conversion getDeclaredAnnotations() This method returns annotations that are directly present on this element getDeclaringClass() This method returns the Class object representing the class or interface that declares the field represented by this Field object getDouble(Object obj ) This method gets the value of a static or instance field of type double or of another primitive type convertible to type double via a widening conversion getFloat(Object obj ) This method gets the value of a static or instance field of type float or of another primitive type convertible to type float via a widening conversion getGenericType() This method returns a Type object that represents the declared type for the field represented by this Field object getInt(Object obj ) This method returns a Type object that represents the declared type for the field represented by this Field object getLong(Object obj ) This method gets the value of a static or instance field of type long or of another primitive type convertible to type long via a widening conversion getModifiers() This method returns the Java language modifiers for the field represented by this Field object, as an integer getName() This method returns the name of the field represented by this Field object getShort(Object obj ) This method gets the value of a static or instance field of type short or of another primitive type convertible to type short via a widening conversion hashCode() This method returns a hashcode for this Field. isEnumConstant() This method returns true if this field represents an element of an enumerated type; returns false otherwise isSynthetic() This method returns true if this field is a synthetic field; returns false otherwise setBoolean(Object obj , boolean z) This method sets the value of a field as a boolean on the specified object setByte(Object obj, byte b) This method sets the value of a field as a byte on the specified object setChar(Object obj, char c) This method sets the value of a field as a char on the specified object setDouble(Object obj, double d) This method sets the value of a field as a double on the specified object setFloat(Object obj, float f) This method sets the value of a field as a float on the specified object setInt(Object obj, int i) This method sets the value of a field as an int on the specified object setLong(Object obj, long l) This method sets the value of a field as a long on the specified object setShort(Object obj, short s) This method sets the value of a field as a short on the specified object toGenericString() This method returns a string describing this Field, including its generic type toString() This method returns a string describing this Field
示例 1:
Java
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws Exception
{
// Create the User class object
User user = new User();
// Get the all field objects of User class
Field[] fields = User.class.getFields();
for (int i = 0; i < fields.length; i++) {
// get value of the fields
Object value = fields[i].get(user);
// print result
System.out.println("Value of Field "
+ fields[i].getName()
+ " is " + value);
}
}
}
// sample User class
class User {
public static String name = "Dipsundar";
public static String getName()
{
return name;
}
public static void setName(String name)
{
User.name = name;
}
}
Java
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args) throws Exception
{
// Create the User class object
User user = new User();
// Get the all field objects of User class
Field[] fields = User.class.getFields();
for (int i = 0; i < fields.length; i++) {
// get value of the fields
Object value = fields[i].get(user);
// print result
System.out.println("Value of Field "
+ fields[i].getName()
+ " is " + value);
}
}
}
// sample User class
class User {
public static String name = "Dipsundar";
public static String getName() { return name; }
public static void setName(String name)
{
User.name = name;
}
}
Value of Field name is Dipsundar
示例 2:
Java
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args) throws Exception
{
// Create the User class object
User user = new User();
// Get the all field objects of User class
Field[] fields = User.class.getFields();
for (int i = 0; i < fields.length; i++) {
// get value of the fields
Object value = fields[i].get(user);
// print result
System.out.println("Value of Field "
+ fields[i].getName()
+ " is " + value);
}
}
}
// sample User class
class User {
public static String name = "Dipsundar";
public static String getName() { return name; }
public static void setName(String name)
{
User.name = name;
}
}
Value of Field booleanValue is false