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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. package com.kxg;

  2. interface Inner
  3. {
  4.         public abstract void show();
  5. }

  6. class Outer5
  7. {
  8.         public Inner method()
  9.         {
  10.                 return
  11.                 new Inner()//返回一个Inner接口的匿名实现类的对象
  12.                 {
  13.                         public void show()
  14.                         {
  15.                                 System.out.println("匿名内部类重写接口方法");
  16.                         }
  17.                 };
  18.         }
  19.         public void method2()//method2里面是Inner接口实现类的对象调用实现类重写接口的方法
  20.         {
  21.                 new Inner()
  22.                 {
  23.                         public void show()
  24.                         {
  25.                                 System.out.println("匿名内部类重写接口方法");
  26.                         }
  27.                 }.show();
  28.         }
  29. }
  30. public class InnerDemo5 {
  31.         public static void main(String[] args)
  32.         {
  33.                 Outer5 o = new Outer5();
  34.                 o.method().show();//o.method()是一个对象
  35.                 o.method2();
  36.         }
  37. }
复制代码


8 个回复

倒序浏览
  1. package com.kxg;

  2. interface Inner2
  3. {
  4.         public abstract void show();
  5.         public abstract void show2();
  6. }

  7. class Outer6
  8. {
  9.         public void method()
  10.         {
  11.                 new Inner2()
  12.                 {
  13.                         public void show()
  14.                         {
  15.                                 System.out.println("show");
  16.                         }
  17.                         public void show2()
  18.                         {
  19.                                 System.out.println("show2");
  20.                         }
  21.                 }.show();
  22.                
  23.                 new Inner2()
  24.                 {
  25.                         public void show()
  26.                         {
  27.                                 System.out.println("show");
  28.                         }
  29.                         public void show2()
  30.                         {
  31.                                 System.out.println("show2");
  32.                         }
  33.                 }.show2();
  34.                
  35.                 //利用多态,用接口类型接收一个接口实现类
  36.                 Inner2 i = new Inner2()
  37.                 {
  38.                         public void show()
  39.                         {
  40.                                 System.out.println("show");
  41.                         }
  42.                         public void show2()
  43.                         {
  44.                                 System.out.println("show2");
  45.                         }
  46.                 };
  47.                 i.show();
  48.                 i.show2();
  49.         }
  50. }
  51. public class InnerDemo6
  52. {
  53.         public static void main(String[] args)
  54.         {
  55.                 Outer6 o = new Outer6();
  56.                 o.method();
  57.         }
  58. }
复制代码
回复 使用道具 举报
  1. package com.kxg;

  2. interface Person
  3. {
  4.         public abstract void study();
  5. }

  6. class PersonDemo
  7. {
  8.         public void method(Person p)
  9.         {
  10.                 p.study();
  11.         }
  12. }

  13. class Student implements Person
  14. {
  15.         public void study()
  16.         {
  17.                 System.out.println("学习");
  18.         }
  19. }

  20. public class InnerDemo7
  21. {
  22.         public static void main(String[] args)
  23.         {
  24.                 //需要创建一个接口类的具体实现类
  25.                 PersonDemo p = new PersonDemo();
  26.                 Student s = new Student();
  27.                 p.method(s);
  28.                
  29.                 //不需要创建具体实现类,直接利用匿名内部类创建匿名对象
  30.                 p.method(new Person()
  31.                 {
  32.                         public void study()
  33.                         {
  34.                                 System.out.println("学习知识");
  35.                         }
  36.                 });
  37.         }
  38. }
复制代码
回复 使用道具 举报
  1. /*
  2.           按照要求,补齐代码
  3.           interface Inter
  4.           {
  5.                   void show();
  6.           }
  7.           class Outer{补齐代码}
  8.           class Outer
  9.           {
  10.           public static void main (String [] ages)
  11.           {
  12.                           Outer.method().show();
  13.           }
  14.           }
  15.           要求在控制台输出“Hello World”
  16.          
  17.           分析:
  18.           1.Outer.method().show()
  19.                   从这里可以看出只有Outer.method()返回值是一个对象,才可以完成后面调用show()方法
  20.           2.method()方法可以直接被类名调用,说明method()方法被静态修饰
  21. */
  22. package com.kxg;

  23.         interface InterTest
  24.         {
  25.                 void show();
  26.         }
  27.         class OuterTest
  28.         {
  29.                 public static InterTest method()
  30.                 {
  31.                         return
  32.                         new InterTest()
  33.                         {
  34.                                 public void show()
  35.                                 {
  36.                                         System.out.println("Hello World");
  37.                                 }
  38.                         };
  39.                 }
  40.         }
  41. class OuterDemo
  42. {
  43.         public static void main (String [] ages)
  44.         {
  45.                         OuterTest.method().show();
  46.         }
  47. }
复制代码
回复 使用道具 举报
表示怎么获得这种格式的文本?

点评

选择插入代码,工具栏中的<>,复制黏贴代码就行了,  发表于 2015-7-2 17:51
回复 使用道具 举报
谢谢。。。。。。。。。。
回复 使用道具 举报
javazhang 发表于 2015-7-2 16:08
表示怎么获得这种格式的文本?
  1. class Array2Demo{
  2. public static void main(String[] args){
  3. int[][] arr = new int[3][2];
  4. //直接打印二维数组
  5. System.out.println(arr);
  6. //直接打印二位数组中的角标为0的一维数组
  7. System.out.println(arr[0]);
  8. //直接打印二维数组中的角标为0的一维数组的角标为0的元素
  9. System.out.println(arr[0][0]);
  10. }
  11. }
复制代码

像这种没有那种1,2,3,4,5那种排列的样子
回复 使用道具 举报
看不懂,努力修行
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马