java编译器会将每一个类编译成一个class文件,那问题来了,内部类和匿名类怎么办,答案就是"$"标识。
package red.sea.daelly.pojo;
import red.sea.daelly.inf.TestInterface;
/**
* @author liqingyang
* @date 2016-4-14 下午1:24:50
*/
public class Outer {
public Outer(){
AnotherOuter anotherOuter = new AnotherOuter();
System.out.println(anotherOuter);
Inner inner = new Inner();
System.out.println(inner);
doSth(new TestInterface() {
@Override
public void hello() {
System.out.println("I am in Outer's Construtor");
}
});
}
public void doSth(TestInterface inf){
inf.hello();
System.out.println(inf);
}
public static class Inner {
}
public static void main(String[] args){
Outer outer = new Outer();
System.out.println(outer);
}
}
class AnotherOuter{
}得到输出
red.sea.daelly.pojo.AnotherOuter@1690726
red.sea.daelly.pojo.Outer$Inner@9931f5
I am in Outer's Construtor
red.sea.daelly.pojo.Outer$1@1f1fba0
red.sea.daelly.pojo.Outer@1befab0
可见,内部类会在$后面加上内部类的类名,匿名类会以数字表示,从1开始
本文探讨了Java中内部类和匿名类的编译机制,详细介绍了编译器如何通过添加'$'标识来区分这些类,并生成对应的.class文件。
2671

被折叠的 条评论
为什么被折叠?



