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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 青衫禅客 中级黑马   /  2014-12-28 22:05  /  674 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

interface Inter
{
        public abstract Inter show();
        //public abstract Inter show2();
}
class Outer
{
        public void method()
        {
                new Inter()
                {
                        public Inter show()
                        {
                                System.out.println("show");
                                return this;
                        }
                        public Inter show2()
                        {
                                System.out.println("show2");
                                return this;
                        }
                }.show().show2();//先执行show()和先执行show2()所返回的对象不一样吗?
        }
}
class AnonymousTest
{
        public static void main(String[] args)
        {
                new Outer().method();
        }
}

评分

参与人数 2黑马币 +17 收起 理由
黑马-李权88 + 6 赞一个!
danielzyj + 11 题目给力

查看全部评分

3 个回复

倒序浏览
把this代表的子类对象的引用给了接口Inter,这是一个多态的过程。不能访问Inter没有的方法。
回复 使用道具 举报
danielzyj 发表于 2014-12-28 22:28
把this代表的子类对象的引用给了接口Inter,这是一个多态的过程。不能访问Inter没有的方法。 ...

你的意思是不能访问show2()方法?
回复 使用道具 举报
这道题目很好。show2和show1返回的对象应该是一样的,可能是它们所属的引用变量不同。我也想知道答案!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马