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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© LPStone 中级黑马   /  2015-5-9 23:13  /  795 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

感觉匿名内部类最难了 ,求大神详解,谢谢!
根据长下文,在提示处补充代码。

interface Inter
{
        void method();
}
class Test
{
          //补足代码。通过匿名内部类。
}
class InnerClassTest
{
        public static void main(String[] args)
        {
                 Test.function().method();
        }
}




评分

参与人数 1黑马币 +2 收起 理由
893269222 + 2 赞一个!

查看全部评分

12 个回复

倒序浏览
public static Inter function(){
      retrun  new Inter(){
             public void method(){
                 System.out.println("HI");
             }
      }

}
回复 使用道具 举报
Test.function().method();
首先Test.function()是由类直接调用的,说明是个静态方法,.method();可以看出返回值类型是Inter,因为只有Inter才能调用method();
回复 使用道具 举报
jiangwenjun 发表于 2015-5-9 23:31
public static Inter function(){
      retrun  new Inter(){
             public void method(){

大神啊,能不能详解一下,我也不是很懂
回复 使用道具 举报
public static Inter function() {
//由Test.function().method();这是一个链式编程,其返回的都是一个对象.
  //首先Test.function()说明functions是一个静态方法        ,同理method()也为静态.

                return new Inter() {    //此段代码可以看成 Inter i = new Test
                                public void method() {
                                        System.out.println("method");
                                }
                        }
                }
回复 使用道具 举报
我干阿衰 发表于 2015-5-10 01:05
public static Inter function() {
//由Test.function().method();这是一个链式编程,其返回的都是一个对象. ...

差个分号。。。
回复 使用道具 举报
static A function(){
  return new A();
  class A implement Inter{
static void  method(){}
}
}

回复 使用道具 举报
public static void function() {
}




回复 使用道具 举报
彭越 中级黑马 2015-5-10 14:41:26
9#
难吗,还可啦!
回复 使用道具 举报
匿名内部类其实没那么难理解,说白了就是一个匿名子类对象,记住格式基本没什么大问题了,格式: new 父类或者接口(){ 定义子类的内容... }
//Test.function()可以推断出,Test类中有一个静态方法Function();
//.method()推出function这个方法返回的是一个对象,并且是一个Inter类型的对象
public static Inter function(){   //静态Inter类型的function
     return new Inter(){            //基本格式 new 接口
        public void method(){
           System.out....
       }
   }
}
回复 使用道具 举报
yihuihua 发表于 2015-5-10 00:24
Test.function().method();
首先Test.function()是由类直接调用的,说明是个静态方法,.method();可以看出 ...

赞!!!!
回复 使用道具 举报
Melo 中级黑马 2015-5-10 22:06:38
12#
大神啊,能不能详解一下,
回复 使用道具 举报
少个分号;
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马