标题:
成员内部类,局部内部类,匿名内部类与Static,final 更新
[打印本页]
作者:
look_w
时间:
2019-3-8 18:52
标题:
成员内部类,局部内部类,匿名内部类与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();
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0