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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hyz123456 中级黑马   /  2013-10-10 17:15  /  1051 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

interface outer
{
        public final String date="2013年12月";
        public abstract void method();
}

class Test
{
        static class inner implements outer //子类对象实现父类
        {
                public void method()        //复写method方法
                {        
                        System.out.println(this.date+"一定去黑马!");
                }
        }
        static outer function()                //静态化方法  函数名直接调用 无需建立对象
        {
                return new inner();                //返回对象  记得返回值类型!!
        }
        static outer function1()               
        {
                return new outer()                                //建立匿名子类对象
                {
                        public void method()
                        {
                                System.out.println("一定成功!");
                        }
                };                                                //记得写分号!
        }
        static void function2()                        
        {
                new Object()                        //匿名子类对象
                {
                        public void method()                        
                        {
                                System.out.println("为梦想而努力奋斗!");
                        }
                }.method();                                //可直接调用子类方法!
        }
}


class InnerTestClass
{
        public static void main(String[] args)
        {
                Test.function().method();        //Test.function()是一个对象  .method()即调用对象方法
                Test.function1().method();                        
                /*相当于new Test().function(){outer a = new inner(); a.method();}*/
                Test.function2();
        }
}

评分

参与人数 1技术分 +1 收起 理由
周志龙 + 1 多将优秀的代码分享出来,让大家学习,赞一个.

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马