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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘宁 中级黑马   /  2012-3-16 01:20  /  4625 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

interface Inter
{
        void method();
}

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

}
public static void main(String[] args)
{
     Test.function().method();
}
在这个匿名类的练习中,有个问题不理解,我看了好多遍还是看不懂,求救。
Test.function():Test类中有个静态的方法function。这个我懂。
.method:function这个方法运行后的结果是一个对象,而且是个Inter的对象。
所以我的问题:返回值为为什么是Inter?

2 个回复

倒序浏览
返回值为为什么是Inter?因为你把函数定义为Inter类型。所以必须返回一个Inter对象。
回复 使用道具 举报
interface Inter
{
        void method();
}

class Test
{
      public static Inter function()
        {
               Inter in = new Inter()
                {
                        public void method()
                        {
                                System.out.println("haha");
                        }
                };
         return in;        给你把代码改一下就清楚了,只是省略了 Inner s = new Inner()    直接把等号后面的全部返回了,不就是一个对象了吗?
        }

}
public static void main(String[] args)
{
     Test.function().method();
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马