Java Class getField()方法
java.lang.Class.getFields() 返回一个包含Field对象反映此Class对象(object)方法所表示的类或接口的所有可访问公共字段的数组返回一个长度为0的数组,如果类或接口没有可利用的公有字段,或者如果它表示一个数组类、基本类型或void。
1 语法
public Field[] getFields() throws SecurityException
2 参数
无
3 返回值
此方法返回一个代表公共字段Field对象的数组。
4 示例
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java Class getField()方法
*/
import java.lang.reflect.*;
public class ClassDemo {
public static void main(String[] args) {
try {
Class cls = Class.forName("java.awt.Label");
System.out.println("Fields =");
// returns the array of Field objects representing the public fields
Field f[] = cls.getFields();
for (int i = 0; i < f.length; i++) {
System.out.println(f[i]);
}
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出结果为:
Fields =
public static final int java.awt.Label.LEFT
public static final int java.awt.Label.CENTER
public static final int java.awt.Label.RIGHT
public static final float java.awt.Component.TOP_ALIGNMENT
public static final float java.awt.Component.CENTER_ALIGNMENT
public static final float java.awt.Component.BOTTOM_ALIGNMENT
public static final float java.awt.Component.LEFT_ALIGNMENT
public static final float java.awt.Component.RIGHT_ALIGNMENT
public static final int java.awt.image.ImageObserver.WIDTH
public static final int java.awt.image.ImageObserver.HEIGHT
public static final int java.awt.image.ImageObserver.PROPERTIES
public static final int java.awt.image.ImageObserver.SOMEBITS
public static final int java.awt.image.ImageObserver.FRAMEBITS
public static final int java.awt.image.ImageObserver.ALLBITS
public static final int java.awt.image.ImageObserver.ERROR
public static final int java.awt.image.ImageObserver.ABORT
热门文章
优秀文章