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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘家斌 中级黑马   /  2014-10-14 18:20  /  1257 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

主函数里面的代码是什么意思?调用的方法不是抽象的吗?
interface Inter
{
        void inter();
}
class Test
{
        public static Inter function()
        {
                return  new Inter()
                {
                        public void inter()
                        {
                                System.out.println("匿名内部类练习");
                        }
                };
        }
}

class  NiMingTest
{
        public static void main(String[] args)
        {
                Test.function().inter();
        }
}

6 个回复

倒序浏览
感觉还没有学到。。。
回复 使用道具 举报
实现了接口里的方法才可以被创建的甚至还可以起名字,   个人理解。
回复 使用道具 举报
不然怎么写。
回复 使用道具 举报
就只是这种写法习惯就好。。。
回复 使用道具 举报
public static void main(String[] args)  为什么要这样写?
回复 使用道具 举报
mmppp 中级黑马 2014-11-26 17:21:27
7#
首先    Test.function(),因为function是静态的,所以用类名调用,没有问题吧。

然后,想一下,你调用了function后得到的是什么,是一个对象 new Inter(),对吧。

这个对象里面有一个方法,叫inter()。所以对象调用方法,不是.inter()吗?

只是连载一起写了 。
其实分开写就是     Test t=new Test;
                     Inter i   =   t.function();
                       i.inter();
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马