Java基础--内部类

凌风博客原创作品。转载请注明出处http://blog.csdn.net/q549130180/article/details/45198669

内部类的访问规则:

1.内部类可以直接访问外部类中的成员,包括私有。
  之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式:外部类名.this
2.外部类要访问内部类,必须建立内部类对象。

访问格式:
1.当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中。
可以直接建立内部类对象
格式:外部类名.内部类 变量名 = 外部类对象.内部类对象;
      Outer.Inner in = new Outer().new Inner();

2.当内部类在成员位置上,就可以被成员修饰符所修饰
比如:private:将内部类在外部类中进行封装。
static:内部类就具备static的特性
当内部类被static修饰后,只能直接访问外部类中的static成员,出现了访问局限


在外部其他类中,如何直接访问static内部类的非静态成员呢?
new Outer.Inner().function(); 


在外部其他类中,如何直接访问static内部类的静态成员呢?
Outer.Inner().function(); 

注意:当内部类中定义了静态成员,该内部类必须是static的
     当外部类中的静态方法访问内部类时,内部类也必须是static的。

内部类定义在局部时:
1.不可以被成员修饰符修饰
2.可以直接访问外部类中的成员,因为还持有外部类中的引用。

   但是不可以访问它所在的局部中的变量。只能访问被final修饰的局部变量


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Outer  
  2. {  
  3.     private int x = 3;  
  4.     class Inner //内部类  
  5.     {  
  6.         int x= 5;  
  7.         void function(){  
  8.             int x = 6;  
  9.             System.out.println("inner:"+x);  
  10.             System.out.println("inner:"+Outer.this.x);  
  11.         }  
  12.     }  
  13.     static class Inner1 //静态内部类  
  14.     {  
  15.           
  16.         void function(){  
  17.           
  18.             System.out.println("inner:");  
  19.               
  20.         }  
  21.     }  
  22.     void method(){  
  23.         Inner in = new Inner();  
  24.         in.function();  
  25.     }  
  26. }  
  27.   
  28. class InnerClassDemo  
  29. {  
  30.     public static void main(String[] args)   
  31.     {  
  32.         //new Outer.Inner1().function();  //直接访问静态内部类的非静态成员  
  33.   
  34.         Outer out = new Outer();  
  35.         out.method();  
  36.   
  37.         //直接访问内部类的成员  
  38.         //Outer.Inner in = new Outer().new Inner();  
  39.         //in.function();  
  40.     }  
  41. }  


匿名内部类:
1.匿名内部类其实就是内部类的简写格式。
2.定义匿名内部类的前提:
内部类必须是继承一个类或者实现接口
3.匿名内部类的格式:new 父类或接口(){定义子类的内容}
4.其实匿名内部类就是一个匿名子类对象,而且这个对象有点胖,可以理解为带内容的对象
5.匿名内部类中定义的方法最好不要超过3个。


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. abstract class AbsDemo  
  2. {  
  3.     abstract void show();  
  4. }  
  5. class Outer  
  6. {  
  7.     int x = 3;  
  8.     /* 
  9.     class Inner extends AbsDemo 
  10.     { 
  11.         void show() 
  12.         { 
  13.             System.out.println("method:"+x); 
  14.         } 
  15.     } 
  16.     */  
  17.     public void function()  
  18.     {  
  19.         //new Inner().show();  
  20.         new AbsDemo()  
  21.         {  
  22.             void show()  
  23.             {  
  24.                 System.out.println("method:"+x);  
  25.             }  
  26.         }.show();  
  27.     }  
  28. }  
  29.   
  30. class InnerClassDemo  
  31. {  
  32.     public static void main(String[] args)   
  33.     {  
  34.           
  35.     }  
  36. }  

练习:


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. interface Inter  
  2. {  
  3.     void method();  
  4. }  
  5. class Test  
  6. {  
  7.     static Inter function(){  
  8.         return new Inter()  
  9.         {  
  10.             public void method(){  
  11.                 System.out.println("method run");  
  12.             }  
  13.         };  
  14.     }  
  15. }  
  16.   
  17. class    
  18. {  
  19.     public static void main(String[] args)   
  20.     {  
  21.         //Test.function():Test类中有一个静态的方法function。  
  22.         //.method:function这个方法运算后的结果是一个对象,而且是一个Inter类型的对象。  
  23.         //因为只有是Inter类型的对象,才可以调用method方法  
  24.         Test.function().method();  
  25.     }  
  26. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值