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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 繁华落幕 中级黑马   /  2015-6-15 22:11  /  856 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 繁华落幕 于 2015-6-24 22:19 编辑

答案 和  讲解  最好有注释
interface Inter
                {
                        void show();
                }

                class Outer
                {
                        //补齐代码,完成主方法中定义的功能
                }

                class Test
                {
                        public static void main(String[] args)
                        {
                                Outer.method().show();
                        }
                }

13 个回复

倒序浏览
  1.     interface Inter
  2.     {
  3.         void show();   
  4.     }

  5.     class Outer
  6.     {
  7.         public static Inter method() {
  8.             class method implements Inter{
  9.                 public void show() {
  10.                 }
  11.             }
  12.             return new method();
  13.         }
  14.     }

  15.     class Test
  16.     {
  17.         public static void main(String[] args)
  18.         {
  19.             Outer.method().show();
  20.         }
  21.     }


复制代码

我唯独内部类学的不好,应该是写错了,我弱到连注释都写不上,这里当练手了,也希望各位大神能给我只出错误!
回复 使用道具 举报
楼上的基本正确。 内部类的命名不规范
回复 使用道具 举报
public static void method()
回复 使用道具 举报
我只是看看
回复 使用道具 举报
首先从这个Outer.method()可以看出method()是类OUT中的静态方法 ,而Outer.method()可以直接调用show();方法,说明Outer.method()运算以后的结果是一个 Inter对象,应为只有Inter对象对象才能调用show()方法
回复 使用道具 举报
此为匿名内部类的写法!!
class Outer
{
        static Inter method()
        {
                return new Inter()
                {
                        public void show()
                        {
                                System.out.println("method run");
                        }
                };
        }

}
回复 使用道具 举报
  1. public class Demo1 {
  2.         public static void main(String[] args)
  3.         {
  4.                 Outer.method().show();
  5.         }
  6. }
  7. interface Inter
  8. {
  9.         void show();
  10. }

  11. class Outer
  12. {
  13.     //补齐代码,完成主方法中定义的功能
  14.          static Inter method(){
  15.                  return new Inter(){
  16.                          public void show()
  17.                          {
  18.                                  System.out.println("show");
  19.                          }
  20.                  };
  21.          }
  22. }
复制代码
回复 使用道具 举报
我只是看看。。。。谢谢楼主分享啊。。。。
回复 使用道具 举报
学习一下
回复 使用道具 举报
本帖最后由 繁华落幕 于 2015-6-16 10:41 编辑

昨天刚搞懂
回复 使用道具 举报
本帖最后由 繁华落幕 于 2015-6-16 10:42 编辑
iFmmer 发表于 2015-6-15 22:21
我唯独内部类学的不好,应该是写错了,我弱到连注释都写不上,这里当练手了,也希望各位大神能给我只出错 ...

嗯嗯  我也是内部类学的不太好  一看到这玩意脑袋就发蒙 唉
回复 使用道具 举报
  1. package inerExam;


  2. interface Inter
  3. {
  4.         void show();
  5. }

  6. class Outer
  7. {
  8.         public static Inter method()
  9.         {
  10.                 return new Inter() {
  11.                                
  12.                                 @Override
  13.                                 public void show() {
  14.                                         // TODO 自动生成的方法存根
  15.                                         System.out.println("show");
  16.                                 }
  17.                         };
  18.         }
  19. }

  20. public class InnerDemo
  21. {
  22.         public static void main(String[] args)
  23.         {
  24.                Outer.method().show();
  25.         }
  26. }
复制代码
回复 使用道具 举报

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