问题描述
我在另一个类中有一个匿名内部类( SomeClass
)。
I have an anonymous inner class inside another class (SomeClass
).
两者 SomeClass.class.getClasses()
和 SomeClass.class.getDeclaredClasses()
返回空数组。
我在 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反射:如何检索匿名内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!