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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© MichaelLian 中级黑马   /  2016-4-15 21:41  /  1148 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class Test_InnerClassAnonymousClass {
  2.         public static void main(String[] args) {
  3.                 Animal.Cat ac=new Animal().new Cat();//内部类需要创建对象才可以调用;
  4.                 ac.print();
  5.                
  6.                 Animal a=new Animal();//局部内部类需要调用类所在的方法;
  7.                 a.method();
  8.         }
  9. }

  10. interface Inter{
  11.         public void method();
  12. }                                                        

  13. class Animal{
  14.         private int num=20;
  15.         class Cat{
  16.                 int num=10;
  17.                 public void print(){
  18.                         System.out.println(this.num);
  19.                         System.out.println(Animal.this.num); //内部类调用外部类成员变量;
  20.                         Dog d=new Dog();        //内部类的之间的相互调用,和类与类之间调用是一样的;
  21.                         d.print();
  22.                 }
  23.         }

  24.         class Dog{
  25.                 int num=30;
  26.                 public void print(){
  27.                         System.out.println(this.num);
  28.                 }
  29.         }

  30.         public void method(){

  31.                 int num=40;

  32.                 class Mouse {                        //局部内部类;
  33.                         final int num=50;
  34.                         public void print(){
  35.                                 System.out.println(this.num);
  36.                         }
  37.                 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
  38.                 Mouse m=new Mouse();        //局部内部类只能在方法内部创建对象调用;
  39.                 m.print();

  40.                 new Inter(){                        //匿名内部类要继承一个类或者接口;
  41.                         public void method(){
  42.                                 System.out.println("AnonymousInnerClass");
  43.                         }
  44.                 };
  45.         }
  46. }
  47.   
复制代码

4 个回复

倒序浏览
你发这个是要表达什么呢?
回复 使用道具 举报
......................
回复 使用道具 举报
你这是要表达什么呢?
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
我懂楼主的意思
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马