日期:2014-05-20 浏览次数:21101 次
public class Main {
public static void main(String[] args) {
InnerTest inner = new InnerTest();
Test t = inner.get(3);
System.out.println(t.getI());
}
}
class Test { //超类
private int i;
public Test(int i) {
this.i = i;
}
public int getI() {
return i;
}
}
class InnerTest { //用于内部类的测试
public Test get(int x) {
return new Test(x) { //创建匿名内部类,调用父类的构造方法
@Override
public int getI() {
return super.getI() * 10;
}
};
}
}
Compiled from "Main.java"
class InnerTest$1 extends Test{
final InnerTest this$0;
InnerTest$1(InnerTest, int);
Code:
0: aload_0
1: aload_1
2: putfield #1; //Field this$0:LInnerTest;
5: aload_0
6: iload_2
7: invokespecial #2; //Method Test."<init>":(I)V
10: return
public int getI();
Code:
0: aload_0
1: invokespecial #3; //Method Test.getI:()I
4: bipush 10
6: imul
7: ireturn
}
Compiled from "Main.java"
class InnerTest extends java.lang.Object{
InnerTest();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public Test get(int);
Code:
0: new #2; //class InnerTest$1
3: dup
4: aload_0
5: iload_1
6: invokespecial #3; //Method InnerTest$1."<init>":(LInnerTest;I)V
9: areturn
}