问题描述
我在另一个类(SomeClass
)中有一个匿名内部类.
I have an anonymous inner class inside another class (SomeClass
).
SomeClass.class.getClasses()
和 SomeClass.class.getDeclaredClasses()
都返回空数组.
Both SomeClass.class.getClasses()
and SomeClass.class.getDeclaredClasses()
return empty arrays.
我在 Class
的 Javadocs 中找不到关于此的一些提示.
I couldn't find some hints on this in Class
' Javadocs.
匿名内部类可以以某种方式使用反射来检索吗?
匿名内部类和普通内部类之间还有哪些显着区别?
推荐答案
如果它使用反射,这可能是一个非常糟糕的主意.撇开这一点不谈,我相信您可以在运行时添加内部类,因此列出可能尚未想到的类是没有意义的.列出当前加载的类,我想,需要通过 Java 代理或类似的.
If it's using reflection, it's probably a really bad idea. Leaving that aside, I believe you can additional inner classes at runtime, so it doesn't make sense to list classes that may not have been thought of yet. Listing currently loaded classes would, I guess, require going through Java agents or similar.
匿名内部类组成了名称、封闭方法和用于复制已复制的外部局部变量的附加合成字段.一个类在运行时与另一个类几乎相同.请记住,1.1 引入了内部类,但类文件自 1.0 以来几乎没有变化.
Anonymous inner classes have made up names, an enclosing method and additional synthetic fields for copying external local variables that have been copied. One class is pretty much the same as another at runtime. Remember that 1.1 introduced inner classes, but class files have barely changed since 1.0.
这篇关于Java 反射:如何检索匿名内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!