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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© pengbin 中级黑马   /  2015-7-20 17:07  /  239 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


/*
interface Inter
{
        void method();
}

class Test
{
        //补足代码,通过匿名内部类。
}

class InnerClassTest
{
        public static void main(String[] args)
        {
                Test.funtion().method();
        }
}


*/

//用内部类实现
/*
interface Inter
{
        void method();
}

class Test
{
        static class Inner implements Inter
        {
                public void method()
                {
                        System.out.println("method run")
                }
        }

        static Inter function()
        {
                return new Inner();
        }
}

class InnerClassTest
{
        public static void main(String[] args)
        {
                Test.funtion().method();
        }
}
*/


//用匿名内部类实现

interface Inter
{
        void method();
}

class Test
{
        static Inter function()
        {
                return new Inter()
                {
                        public void method()
                        {
                                System.out.println("method run");
                        }
                };
        }
}

class InnerClassTest
{
        public static void main(String[] args)
        {
                //Test.function():看到这个可以知道Test类中有一个静态的方法function();
                //.method():说明function()运算后得到的是一个对象,而且是一个Inter类型的对象。
                //因为只有是Inter类型的对象,才可以调用method方法!
                Test.function().method();
        }
}

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马