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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄泉 中级黑马   /  2014-4-3 16:20  /  1062 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

看毕老师视频。有一段练习。关于我的做法和大家分享下!
  1. interface Inter
  2. {
  3.         void show(int a ,int b)
  4.         void func();
  5. }

  6. class Demo
  7. {
  8.         public static void main(String[] args)
  9.         {
  10.                 //补足代码,调用两个函数,要求是匿名内部类
  11.         }
  12. }
复制代码
老师给出的答案是匿名内部类的子父类方式,在外部调用show()与func()
  1. interface Inter
复制代码

1 个回复

倒序浏览

  1. interface Inter
  2. {
  3.         void show(int a ,int b)
  4.         void func();
  5. }

  6. class Demo
  7. {
  8.         public static void main(String[] args)
  9.         {
  10.                 //补足代码,调用两个函数,要求是匿名内部类
  11.                 Inter i =new Inter()
  12.                 {
  13.                         void show(int a ,int b)
  14.                         {
  15.                        
  16.                         }
  17.                         void func()
  18.                         {
  19.                                
  20.                         }
  21.                 };
  22.                 i.show();//毕老师是通过子父类的形式调用show()和func()
  23.                 i.func();
  24.         }
  25. }
复制代码
我的是

  1. interface Inter
  2. {
  3.         void show(int a ,int b)
  4.         void func();
  5. }

  6. class Demo
  7. {
  8.         public static void main(String[] args)
  9.         {
  10.                 //补足代码,调用两个函数,要求是匿名内部类
  11.                 new Inter()
  12.                 {
  13.                         void show(int a ,int b)
  14.                         {
  15.                                 System.out.println(a+b);
  16.                         }
  17.                         void func()
  18.                         {
  19.                                 show(4,5);
  20.                         }
  21.                 }.func();//我是偷了个机 利用func()来调用show(),在show()的内部定义方法
  22.         }
  23. }
复制代码


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