gpt4 book ai didi

java - 通过反射获取子类字段

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:35 37 4
gpt4 key购买 nike

我有 2 个类:B 扩展 A

我在类 B 上使用 getDeclaratedFields(),但没有从类 `A 获取字段。

我怎样才能得到它们?

最佳答案

getDeclaredFields() 返回由类而不是父类(super class)声明的字段。

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.

使用 getFields 方法检索类和父类(super class)声明的字段。

Specifically, if this Class object represents a class, this method returns the public fields of this class and of all its superclasses

阅读Java Class了解更多信息。

关于java - 通过反射获取子类字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15334310/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com