This answer另一个问题表明,作为通过反射查找匿名类的解决方法,只需尝试所有名称,从 ...$1
开始并向上计数,直到找不到更多名称。这是否保证找到所有内部类,或者是否存在它们从 0 开始或某些数字被遗漏(无论出于何种原因)的情况?
最佳答案
JLS 13.1指定:
The class or interface must be named by its binary name, which must meet the following constraints:
- The binary name of a top level type (§7.6) is its canonical name (§6.7).
- [...]
- The binary name of an anonymous class (§15.9.5) consists of the binary name of its immediately enclosing type, followed by $, followed by a non-empty sequence of digits.
所以从理论上讲,它不必从 1 开始,但它必须类似于 EnclosingClass$N
,其中 N 是一个数字。
关于java - 是否在任何地方指定匿名类的数量从 1 开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12267459/