gpt4 book ai didi

java - 是否可以将类表示为字段?

转载 作者:行者123 更新时间:2023-12-02 02:50:04 25 4
gpt4 key购买 nike

我需要检索类内的注释。想象一下:

@MyAnnotation(MyClass.class)
public Table myTable;

我需要做的是从注释内的类初始化 Table 类内的变量。问题是,如何将 Table 类表示为字段,以便从注释中检索值,如下所示:

Field tableField = ? // Here I should somehow cast 'this' to type Field
Class annotationValue = tableField.getAnnotation(MyAnnotation.class).value();

是否可以以某种方式将 Table 类转换为 Field 类?

最佳答案

如果您只想将 myTable 字段作为 Field 对象访问,您可以编写:

Field tableField = Enclosing.class.getDeclaredField("myTable");

其中 Enclosure 是声明 myTable 字段的类的名称。

关于java - 是否可以将类表示为字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43996270/

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