getfields
类的类getFields()方法 (Class class getFields() method)
getFields() method is available in java.lang package.
getFields()方法在java.lang包中可用。
getFields() method is used to return an array of Field objects that indicate all the public Fields of the class or interface denoted by this Class object.
getFields()方法用于返回一个Field对象数组,这些对象指示此Class对象表示的类或接口的所有公共Field。
getFields() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
getFields()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
getFields() method may throw an exception at the time of returning an array of Field objects.
在返回Field对象数组时, getFields()方法可能会引发异常。
SecurityException: In this exception, it may raise when the security manager exists.
SecurityException :在此异常中,当安全管理器存在时可能会引发此异常。
Syntax:
句法:
public Field[] getFields();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of this method is Field[], it returns an array of Field object denoting all the public field of this class or an interface.
此方法的返回类型为Field [] ,它返回一个Field对象数组,该数组表示此类或接口的所有公共字段。
Note:
注意:
When class or interface does not contain public field, 0 is returned.
当类或接口不包含公共字段时,将返回0。
When this class object holds primitive, an array class, or void type, 0 is returned.
当此类对象包含基本类型,数组类或void类型时,将返回0。
Example:
例:
// Java program to demonstrate the example
// of Field[] getFields () method of Class
import java.lang.reflect.*;
public class GetFieldsOfClass {
public static void main(String[] args) throws Exception {
GetFieldsOfClass fields = new GetFieldsOfClass();
// Get Class
Class cl = fields.getClass();
// It returns an array of Field objects that indicate
// public fields represented by the Class
Field[] f = cl.getFields();
for (int i = 0; i < f.length; ++i)
System.out.println("Public Fields: " + f[i].toString());
}
// Private Constructors
private GetFieldsOfClass() {
System.out.println("We are in private constructor");
}
// Public Constructors
public GetFieldsOfClass(int i, short s) {
this.i = i;
this.s = s;
}
private int i = 100;
public short s = 200;
}
Output
输出量
We are in private constructor
Public Fields: public short GetFieldsOfClass.s
翻译自: https://www.includehelp.com/java/class-class-getfields-method-with-example.aspx
getfields