成员内部类,局部内部类,匿名内部类与Static,final 更新
- UID
- 1066743
|
成员内部类,局部内部类,匿名内部类与Static,final 更新
局部内部类,局部内部类不允许静态修饰
public class InnerClassDemo {
int i;
public static void main(String[] args) {
InnerClassDemo innerClassDemo = new InnerClassDemo();
innerClassDemo.show();
}
public void show() {
class TestDemo {
public void showInner() {
System.out.println(i);
}
}
TestDemo testDemo = new TestDemo();
testDemo.showInner();
}
}
局部内部类反编译
public class InnerClassDemo
{
int i;
public InnerClassDemo()
{
}
public static void main(String args[])
{
InnerClassDemo innerClassDemo = new InnerClassDemo();
innerClassDemo.show();
}
public void show()
{
class 1TestDemo
{
final InnerClassDemo this$0;
public void showInner()
{
System.out.println(i);
}
1TestDemo()
{
this.this$0 = InnerClassDemo.this;
super();
}
}
1TestDemo testDemo = new 1TestDemo();
testDemo.showInner();
}
}
匿名内部类
public class InnerClassDemo {
public static void main(String[] args) {
InnerClassDemo innerClassDemo = new InnerClassDemo();
innerClassDemo.show();
}
public void show() {
final int i = 10;
new TestDemo1() {
@Override
public void showInner() {
System.out.println(i);
}
}.showInner();
}
}
abstract class TestDemo1 {
public abstract void showInner();
} |
|
|
|
|
|