黑马程序员技术交流社区

标题: 匿名内部类作参数传递 [打印本页]

作者: ypenghui    时间: 2016-5-14 21:59
标题: 匿名内部类作参数传递
实现以下代码:
  1. class test{
  2.    public static void main(String[] args){
  3.    outer.method().show();
  4. //分析outer.method()为对象;show为方法
  5. //outer.静态方法
  6.    }
  7. }
  8. interface Inter{
  9.   void show();
  10. }
  11. class outer{
  12. //补齐以下代码
  13.    public static Intter method(){
  14.       return new Inter(){
  15.         public  void show(){}
  16.       };
  17.    }
  18. }
复制代码




作者: yaolv7    时间: 2016-5-14 22:10
哥们,看清楚,别激动,Inter不是Intter.....

还有,类名单词首字母记得大写....

不一定非要用Inter

  1. class Outer{
  2.                 //补齐以下代码
  3.                    public static Outer method(){
  4.                       return new Outer(){
  5.                         public void show(){}
  6.                       };
  7.                    }
  8.                    public void show(){}
  9.                 }
复制代码

  1. class Outer{
  2.                 //补齐以下代码
  3.                    public static Inner method(){
  4.                       return new Inner(){
  5.                         public void show(){}
  6.                       };
  7.                    }
  8.                   
  9.                    static class Inner {
  10.                            void show(){}
  11.                    }
  12.                 }
复制代码


其实原理是一样的,都是返回一个匿名类
作者: ypenghui    时间: 2016-5-14 22:27
对的,谢谢大神指点




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