getfields_Java类类getFields()方法及示例

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java反射机制可以通过Class类来获取一个类的信息,包括类的名称、属性、方法等。以下是一个示例,演示如何使用Java反射来获取类的信息: ```java import java.lang.reflect.Field; import java.lang.reflect.Method; public class ReflectionDemo { public static void main(String[] args) throws Exception { // 获取Person类的Class对象 Class personClass = Person.class; // 获取Person类的public属性 Field[] fields = personClass.getFields(); for (Field field : fields) { System.out.println(field.getName()); } // 获取Person类的所有属性 Field[] declaredFields = personClass.getDeclaredFields(); for (Field field : declaredFields) { System.out.println(field.getName()); } // 获取Person类的public方法 Method[] methods = personClass.getMethods(); for (Method method : methods) { System.out.println(method.getName()); } // 获取Person类的指定方法 Method method = personClass.getMethod("eat", String.class); System.out.println(method.getName()); // 创建Person对象 Person person = new Person(); // 调用Person对象的eat方法 method.invoke(person, "apple"); } } class Person { public String name; private int age; public void eat(String food) { System.out.println("I am eating " + food); } } ``` 在上面的示例中,我们首先获取了Person类的Class对象,然后使用Class对象的getFields和getDeclaredFields方法获取类的属性信息,使用getMethods方法获取类的方法信息,使用getMethod方法获取指定方法的信息。最后,我们创建了一个Person对象,并使用反射机制调用了Person对象的eat方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值