更新时间:2023-02-15 20:10:23
在字节码级别上,内部类只是普通的Java类.由于Java字节码验证程序不允许访问私有成员,因此它将为您使用的每个私有字段生成综合访问器方法.另外,为了将内部类与其封闭的实例链接起来,编译器将合成指针添加到外部"this".
On bytecode level inner classes are just plain Java classes. Since the Java bytecode verifier does not allow access to private members, it generates synthetic accessor methods for each private field which you use. Also, in order to link the inner class with its enclosing instance, the compiler adds synthetic pointer to the outer 'this'.
考虑到这一点,内部类只是语法糖的一层.它们很方便,您已经列出了一些要点,所以我将列出一些您可能要考虑的负面方面:
Considering this, the inner classes are just a layer of syntax sugar. They are convenient and you have listed some good points, so I'd list some negative aspects which you might want to consider:
P.S.我说的是非平凡的内部类(尤其是那些没有实现任何接口的内部类).三行侦听器实现是好的.
P.S. I'm talking about non-trivial inner classes (especially ones that do not implement any interfaces). Three line listener implementations are good.