A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yiluzuolai 中级黑马   /  2016-5-23 23:33  /  314 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文



interface Inter {

             public abstract void function();

     }

     // 外部类
     class Outer {
             public Inter method() {
                 final int i=10;//局部变量i在栈中存在,当method()方法执行结束被销毁
                               //加final目的是为了延长i的生命周期
                               //加final修饰i的i不放在栈中,放在常量池中
                               //function才能访问i
                     class InnerImpl implements Inter {
                             public void function() {
                    System.out.println(i);
                             }

                     }
                     return new InnerImpl();
             }

     }

     class FinalVariable {

             public static void main(String[] args) {

                     Outer out = new Outer();

                     Inter it = out.method();
            
                     it.function();
             }

     }

1 个回复

倒序浏览
顶贴是一种美德
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马