@
amiwrong123 别纠结这些定义吧。其实静态内部类也可以是没名字的啊。
```
public enum MyEnum {
A {
void doSomething() { }
},
B {
void doSomethingElse() { }
};
static class SS {
}
public void countDown(){
new Thread(){
@
Override public void run() {
}
}.start();
}
public static void main(String[] args) {
System.out.println(MyEnum.A.getDeclaringClass());
System.out.println(MyEnum.A.getClass());
System.out.println(MyEnum.A.getClass().getSuperclass());
SS s = new SS();
System.out.println(s.getClass());
System.out.println(s.getClass().getSuperclass());
System.out.println(Enum.valueOf(MyEnum.class,"A"));
}
}
```
你看这段代码运行后会生成 MyEnum$3.class,就是你指的所谓的”匿名内部类“。你会发现底层实现不区分这些区别。没有指定名字的类就是从 1 开始编排,如果你是 static 就不会传外部类的引用,不是 static 就传引用。
class org.bupt.pms.consistence.MyEnum$3 extends java.lang.Thread {
final org.bupt.pms.consistence.MyEnum this$0;
org.bupt.pms.consistence.MyEnum$3(org.bupt.pms.consistence.MyEnum); //看这里
Code:
0: aload_0
1: aload_1
2: putfield #1 // Field this$0:Lorg/bupt/pms/consistence/MyEnum;
5: aload_0
6: invokespecial #2 // Method java/lang/Thread."<init>":()V
9: return
public void run();
Code:
0: return
}