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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 邱俊杰 中级黑马   /  2012-4-11 23:07  /  1888 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package day9;

  2. public class Text_2 {
  3.         public static void main(String[] args)
  4.         {
  5.                 Text.function().method();
  6.         }
  7.        
  8. }


  9. interface Inter
  10. {
  11.         void method();
  12. }
  13. class Text
  14. {
  15.         /*
  16.         static class Inner implements Inter
  17.         {
  18.                 public void method()
  19.                 {
  20.                         System.out.println("run");
  21.                 }
  22.         }
  23.         */
  24.        
  25.         static Inter function()
  26.         {
  27.                 return new Inter()              //这里的return是返回什么?  不能理解
  28.                 {
  29.                         public void method()
  30.                         {
  31.                                 System.out.println("run");
  32.                         }
  33.                 };
  34.         }
  35. }
复制代码

5 个回复

倒序浏览
这里的return是返回什么?  不能理解
返回的是一个带实现体的对象

点评

return 现在可以这样用啊、因为之前都是接触返回值啊,true 和false的,呵呵  发表于 2012-4-11 23:16
回复 使用道具 举报
               return new Inter()              //这里的return是返回什么
              {
                       public void method()
                       {
                               System.out.println("run");
                       }
               };

返回的是一个实现了Inter接口的子类对象,写法比较特殊.
回复 使用道具 举报
  1.         static Inter function()

  2.         {

  3.                 return new Inter()              //这里的return是返回什么?  不能理解

  4.                 {

  5.                         public void method()

  6.                         {

  7.                                 System.out.println("run");

  8.                         }

  9.                 };
复制代码
static Inter function()
从这就可以看出返回值的类型是Inter
return返回的是一个匿名内部类,
实际上就是接口Inter的匿名子类的对象

return返回的一个
回复 使用道具 举报
返回的是Inter类型的对象,你的主函数里面调用这样写Text.function().method();所以下面才有个返回值,即Inter类型的对象。如果你不想返回就像下面的改一下。

public class Text_2 {
        public static void main(String[] args)
        {
                Text.function();
        }
        
}


interface Inter
{
        void method();
}
class Text
{
        /*
        static class Inner implements Inter
        {
                public void method()
                {
                        System.out.println("run");
                }
        }
        */
        
        static void function()
        {
                 new Inter()              //这里的return是返回什么?  不能理解
                {
                        public void method()
                        {
                                System.out.println("run");
                        }
                }.method();
        }
}
回复 使用道具 举报

public class Text_2 {

        public static void main(String[] args)

        {

                Text.function().method();

        }

        

}





interface Inter

{

        void method();

}

class Text

{

        /*

        static class Inner implements Inter

        {

                public void method()

                {

                        System.out.println("run");

                }

        }

        */

        

        static Inter function()

        {

                return new Inter()              //这里的return是返回什么?  不能理解

                {

                        public void method()

                        {

                                System.out.println("run");

                        }

                };

        }

}

知识点总结:
      1.接口有静态成员变量和抽象方法组成!其中变量有static fina修饰,默认可以不添加,方法均为抽象方法,不能实现只能声明(这一天注意和abstract相区别,abstract class中可以有非抽象方法)
      2.这里比较难懂的是   接口不能实例化!
         static Inter function()

        {

                return new Inter()            
                {

                        public void method()

                        {

                                System.out.println("run");

                        }

                };

        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马