首页 > 编程语言 > 详细

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

时间:2021-02-08 10:40:51      阅读:26      评论:0      收藏:0      [点我收藏+]

5.局部内部类Local inner class  

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

例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();
    }
}
 

更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44639795/article/details/103110007

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

原文:https://www.cnblogs.com/shituxingzhe1949/p/14387602.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!