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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 我为你着迷 金牌黑马   /  2014-10-30 16:01  /  1057 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 我为你着迷 于 2014-10-30 16:03 编辑
  1. interface Inter
  2. {
  3.         void method();        
  4. }

  5. class Test
  6. {
  7.         //补足代码。通过匿名内部类。        
  8.         
  9.         /*
  10.         //有名字的,使用内部类形式
  11.         static class Inner implements        Inter
  12.         {
  13.                 public void method()
  14.                 {
  15.                         System.out.println("method run");
  16.                 }        
  17.         }
  18.         
  19.         static Inter function()
  20.         {
  21.                 return new Inner();        
  22.         }
  23.         */
  24.         
  25.         static Inter function()
  26.         {
  27.                  return new Inter()
  28.                  {
  29.                           public void method()
  30.                     {
  31.                             System.out.println("method run");
  32.                     }                        
  33.                  };        
  34.         }
  35. }

  36. class         InnerClassTest
  37. {
  38.         public static void main(String[] args)
  39.         {
  40.                 Test.function().method();
  41.                
  42.                 /*
  43.                 Inter in=Test.function();
  44.                 in.method();
  45.                 */
  46.         }
  47. }
复制代码
.method():代表的是function这个方法运算后的结果是一个对象,而且是一个Inter类型的对象。
大伙好  我的问题是为什么返回的是一个Inter类型的对象呢 这句话该怎么理解啊  。我始终想不明白啊  没救了


评分

参与人数 1黑马币 +1 收起 理由
杨佳名 + 1

查看全部评分

6 个回复

倒序浏览
能调用method方法的只能是实现了Inter的类创建的对象
回复 使用道具 举报
看这代码有点痛苦、、、
回复 使用道具 举报
何丛 发表于 2014-10-30 16:08
能调用method方法的只能是实现了Inter的类创建的对象

那为什么返回的不是Inter创建的对象呢
回复 使用道具 举报
何丛 发表于 2014-10-30 16:08
能调用method方法的只能是实现了Inter的类创建的对象

Inter是一个接口,只能用它的实现类来实例化
回复 使用道具 举报
代码比较麻烦,看一会
回复 使用道具 举报
代码比较麻烦,看一会
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马