java中什么是局部内部类Local inner class?

5.局部内部类Local inner class  

什么叫局部内部类?内部类声明位置:1.它的外部类的范围之内。2.在几个程序块的范围之内。例如,由方法定义的块中或甚至在for循环体内部。局部内部类有什么意义?意义就是:你希望这个类只被解决某个问题用,任何其他人,其他地方都不能用它。就像临时变量一样。生活中百年不遇我们去海边玩,专门裁出一块布来铺在沙滩上, 但这块布干什么别的事都不合适,就属于这种情况。 

例2.5---

class ShellMark_to_win {
    int x = 100;
    void test() {
        for (int i = 0; i < 2; i++) {
/*马克-to-win:for循环之外,Core类不存在。 outside of this for loop, inner can not be used. */
            class Core {
                void display() {
                    System.out.println("外部类的x=" + x);
                }
            }
            Core inner = new Core();
            inner.display();
        }
    //    Core inner = new Core(); //错误找不到Core。
    }
}
public class Test {
    public static void main(String args[]) {
        ShellMark_to_win s = new ShellMark_to_win();
        s.test();
    }
}

更多请见:http://www.mark-to-win.com/tutorial/java_4_LocalInnerClass.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值