黑马程序员技术交流社区

标题: 匿名内部类练习的一个问题、 [打印本页]

作者: 李阳阳    时间: 2013-3-18 13:43
标题: 匿名内部类练习的一个问题、
本帖最后由 李阳阳 于 2013-3-18 19:56 编辑
  1. interface Inter
  2. {
  3.         abstract void method();
  4. }
  5. class Test
  6. {
  7.         //补足代码,通过匿名内部类
  8.         static class inner implements Inter
  9.         {
  10.                 public  void method()
  11.                 {
  12.                         System.out.println("method run");
  13.                 }
  14.         }
  15.         static inner fuction()
  16.         {
  17.                 return new inner();//这样不是也行吗?
  18.         }
  19. }
  20. class InnerClassTest
  21. {
  22.         public static void main(String[] args)
  23.                 {
  24.                         //Test.function():Test类中有一个静态的方法function。
  25.                 //.method():function这个方法运算后的结果是一个对象。而且是一个Inter类型的对象。
  26.                 //因为只有是Inter类型的对象,才可以调用method方法。

  27.                                 Test.fuction().method();   
  28.         }
  29. }
复制代码
主函数内注释部分前两个我知道,但是Test.fuction()运算之后为什么只有Inter类型的对象才能调用method方法呢,Inter不是一个接口不能创建对象吗?所以调用method方法的应该是一个实现接口的类,这里也就是内部类Inner,所以Test.fuction()之后不是应该返回的是一个内部类对象吗?不知道是那个点儿转不回来了,大家给讲讲、
作者: 赵晓东    时间: 2013-3-18 14:35
  1. interface Inter //定义了一个Inter接口
  2. {
  3.         abstract void method(); //接口中的抽象方法method();
  4. }
  5. class Test //外部类
  6. {
  7.         static class Inner implements Inter //内部类,inner 实现了接口 Inter
  8.         {
  9.                 public  void method() //重写了方法 method
  10.                 {
  11.                         System.out.println("method run");
  12.                 }
  13.         }
  14.         static Inner fuction() //这个是外部类Test的静态方法
  15.         {
  16.                 return new Inner(); //返回的是一个Inner对象
  17.         }
  18. }
  19. class InnerClassTest
  20. {
  21.         public static void main(String[] args)
  22.         {
  23.                         Test.fuction().method();
  24.                         //实现步骤是:
  25.                         //外部类Test调用了静态方法fuction()创建了一个内部类对象Inner
  26.                         //Inner对象调用自己从父类接口重写的方法method();
  27.         }
  28. }
  29. //还有就是楼主记住类名一般首字母大写啊
复制代码

作者: 张君    时间: 2013-3-18 14:52
本帖最后由 张君 于 2013-3-18 14:55 编辑

interface Inter
{
        abstract void method();
}
class Test
{
        //补足代码,通过匿名内部类
        static class inner implements Inter
        {
                public  void method()
                {
                        System.out.println("method run");
                }
        }
        static inner fuction()
        {
                return new inner();//这 Test.fuction()运算之后为什么只有Inter类型的对象才能调用method方法呢         }
}
class InnerClassTest
{
        public static void main(String[] args)
                {
                        //Test.function():Test类中有一个静态的方法function。
                //.method():function这个方法运算后的结果是一个对象。而且是一个Inter类型的对象。
                //因为只有是Inter类型的对象,才可以调用method方法。

                                Test.fuction().method();   
        }
}

你的问题是红字部分吧?
因为 Test.fuction()运算之后 返回一个 new inner()对象, 而这inner() 对象 实现了 inter接口。又复写了 method();  所以才可以调用method 方法、
作者: 郑元皓    时间: 2013-3-18 15:06
你这个问题。我是这样理解的。(但是Test.fuction()运算之后为什么只有Inter类型的对象才能调用method方法呢,Inter不是一个接口不能创建对象吗?)
创建对象的时候首字母规范是用大写的。inter很容易让人和Inter混乱的。这里我把你的inter对象改成InterA
Inter是一个接口。但是这里应该是InterA创建的对象。而不是Inter。你创建了InterA对象以后就会调用Inter这个内部类的方法method();(这个方法是重写了Inter接口的)
Test.fuction().method();   //外部类调用fuction()方法。fuction方法new了InterA对象。调用了method方法。输出了method run


这些都是我个人理解,不知道楼主能看懂不。
作者: 李尧    时间: 2013-3-18 15:14
因为method方法 inner本身是不具备的 通过实现Inter接口才得到这个方法.
Inter是个接口,不能实例化,必须通过实现类对象(或者理解成子类对象吧...).这是接口得多态性. 我觉得改成只有实现Inter的类才能调用method方法会好理解一点.
根据你的代码
static inner fuction()
        {
                return new inner();//这样不是也行吗?
        }
Test.fuction()之后不是应该返回的确实就是一个inner对象..不知道楼主在纠结什么..是不是自己给自己绕进去了....
作者: 李阳阳    时间: 2013-3-18 19:55
李尧 发表于 2013-3-18 15:14
因为method方法 inner本身是不具备的 通过实现Inter接口才得到这个方法.
Inter是个接口,不能实例化,必须通 ...

嗯嗯,的确,现在明白过来了,谢谢、




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2