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


笔记如下,自己编写时出现的问题在最后:L ,应该是个小错误,不过可能是我写的代码少吧

  1. interface Inter
  2. {
  3. void method();
  4. }


  5. class Test
  6. {
  7.         //补足代码,通过匿名内部类
  8.         /*
  9.         static class Inner implements Inter
  10.         {
  11.                 public void method()
  12.                 {
  13.                         System.out.println("method");
  14.                 }
  15.         }
  16.         */
  17.        
  18.         static Inter function()   
  19.         {
  20.                 //return new Inner();
  21.                 return new Inter()
  22.                 {
  23.                         public void method()
  24.                         {
  25.                                 System.out.println("NiMing");
  26.                         }
  27.                 };
  28.         }
  29. }


  30. class InnerClassDemoTest
  31. {
  32.         public static void main(String[] args)
  33.         {
  34.                 //Test.function():Test类中有一个静态方法function
  35.                 //.method():function这个方法运算后的结果是一个对象,而且是一个Inter类型的对象。
  36.                 //因为只有是Inter类型的对象,才可以调用method方法
  37.                 Test.function().method();
  38.                
  39.                 Inter in=Test.function();
  40.                 in.method();
  41.         }
  42. }

  43. //在static Inter function()   返回Inter上犯错,写成void,同时没有return,就没有把对象地址返回给main函数
  44. //这就使Test.function().method();变成了null.method();所以程序出错
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马