// Outer.java
public class Outer {
public abstract class Inner {
abstract void test();
}
}
// Client.java
public class Client {
private final Object obj = new Object();
public void run() {
Outer.Inner inner = new Outer().new Inner() {
@Override
void test() {
System.out.println(obj.toString());
}
};
}
}
这里是 build/classes/java/main/Client.class
里的代码:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
import Outer.Inner;
public class Client {
private final Object obj = new Object();
public Client() {
}
public void run() {
Outer var10003 = new Outer();
var10003.getClass();
Inner var10000 = new Inner(var10003) {
{
x0.getClass();
}
void test() {
System.out.println(Client.this.obj.toString());
}
};
}
}
这里是 Client.class
对应的字节码:
// class version 52.0 (52)
// access flags 0x21
public class Client {
// compiled from: Client.java
// access flags 0x0
INNERCLASS Client$1 null null
// access flags 0x401
public abstract INNERCLASS Outer$Inner Outer Inner
// access flags 0x12
private final Ljava/lang/Object; obj
// access flags 0x1
public <init>()V
L0
LINENUMBER 1 L0
ALOAD 0
INVOKESPECIAL java/lang/Object.<init> ()V
L1
LINENUMBER 3 L1
ALOAD 0
NEW java/lang/Object
DUP
INVOKESPECIAL java/lang/Object.<init> ()V
PUTFIELD Client.obj : Ljava/lang/Object;
RETURN
L2
LOCALVARIABLE this LClient; L0 L2 0
MAXSTACK = 3
MAXLOCALS = 1
// access flags 0x1
public run()V
L0
LINENUMBER 6 L0
NEW Client$1
DUP
ALOAD 0
NEW Outer
DUP
INVOKESPECIAL Outer.<init> ()V
DUP
INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class;
POP
INVOKESPECIAL Client$1.<init> (LClient;LOuter;)V
ASTORE 1
L1
LINENUMBER 12 L1
RETURN
L2
LOCALVARIABLE this LClient; L0 L2 0
LOCALVARIABLE inner LOuter$Inner; L1 L2 1
MAXSTACK = 5
MAXLOCALS = 2
// access flags 0x1008
static synthetic access$000(LClient;)Ljava/lang/Object;
L0
LINENUMBER 1 L0
ALOAD 0
GETFIELD Client.obj : Ljava/lang/Object;
ARETURN
L1
LOCALVARIABLE x0 LClient; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
}
最后是老师视频里的代码截图:
我的疑惑是,似乎我并没有在字节码或者 Client.class 文件里看到和 Client$1 一样的代码?