Java内部类

JAVA内部类:定义在另一个类内部的类,内部类分为静态类、匿名类、成员类、局部类。当描述事物时,事物的内部还有事物,该事物用内部类来描述。


不使用内部类来实现

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Outer  
  2. {  
  3.     int x = 2;  
  4. }  
  5. class Inner  
  6.     {  
  7.         void method()  
  8.         {  
  9.         System.out.println("method="+new Outer().x);  
  10.         }  
  11.     }  
  12. class InnerClassDemo5  
  13. {  
  14.     public static void main(String[] args)  
  15.     {  
  16.         new Inner().method();  
  17.     }  
  18. }  


在上例中没有使用内部类,Inner如果要调用Outer的成员,则需要先创建对方的对象,而使用内部类的好处就是内部类可以直接访问外部类中的成员(包括私有),而不用建立外部类的对象。
注: 外部类要访问内部类时,仍须建立内部类对象。


成员内部类

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Outer  
  2. {  
  3.     Private int x = 3;  
  4.       
  5.     class Inner     //内部类在外部类成员位置时可以被private、static修饰  
  6.     {  
  7.         void method()  
  8.         {  
  9.         System.out.println("method"+x);    
  10.         }  
  11.     }  
  12.     void function()  
  13.     {  
  14.         new Inner().method();  
  15.     }  
  16. }  
  17. class InnerClassDemo4  
  18. {  
  19.     public static void main(String[] args)  
  20.     {  
  21.         Outer.Inner in = new Outer().new Inner();  
  22.         in.method();  
  23.         // new Outer().function();  
  24.     }  
  25. }  

Outer.Inner in = new Outer().new Inner();  可以用来生成内部类的对象,格式为:

外部类名.内部类名变量名 =外部类对象.内部类对象;

此方法较少用到,因为我们一般将内部类private封装,并提供一个方法供外部其他类调用内部类的成员。

注意:必须先有外部类的对象才能生成内部类的对象,因为内部类的作用就是为了访问外部类中的成员变量。

程序编译过后会产生两个.class文件,分别是Outer.class和Outer$Inner.class,所以内部类的成员变量/方法名可以和外部类的相同。

 

内部类中的变量访问形式

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:12px;">class Outer  
  2. {  
  3.     private int x = 3;  
  4.       
  5.     private class Inner   
  6.     {  
  7.         int x = 4;  
  8.         void method()  
  9.         {  
  10.         int x = 5;  
  11.          System.out.println("局部变量:"+x);  
  12.           System.out.println("内部类变量:"+this.x);  
  13.           System.out.println("外部类变量:"+Outer.this.x);  
  14.         }  
  15.     }  
  16.     void function()  
  17.     {  
  18.         new Inner().method();  
  19.     }  
  20. }  
  21. class InnerClassDemo9  
  22. {  
  23.     public static void main(String[] args)  
  24.     {  
  25.         new Outer().function();  
  26.     }  
  27. }  
  28. </span>  


运行结果:

局部变量:5

内部类变量:4

外部类变量:3

内部类在没有同名成员变量和局部变量的情况下,内部类会直接访问外部类的成员变量,这是因为省略了Out.this.属性名,否则,内部类中的局部变量会覆盖外部类的成员变量;

访问内部类本身的成员变量可用this.属性名,访问外部类的成员变量需要使用Out.this.属性名。

静态内部类

静态类作为类的静态成员存在于某个类中,定义静态类只需在类的定义中加入关键字static

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Outer  
  2. {  
  3.     Private static int x = 3;       
  4.       
  5.     static class Inner  
  6.     {  
  7.         void method()  
  8.         {  
  9.         System.out.println("method"+x);    
  10.         }  
  11.     }  
  12. }  
  13. class InnerClassDemo5  
  14. {  
  15.     public static void main(String[] args)  
  16.     {  
  17.         Outer.Inner in = new Outer.Inner();  // Outer.Inner()为直接通过外部类类名调用的内部类  
  18.         in.method();  
  19.     }  
  20. }  

在类Outer中定义了静态类Inner后,可将静态类Inner看成类Outerstatic成员。

当内部类被static修饰后,只能直接访问外部类中的static成员,出现了访问局限。

因为内部类被静态化,Static成员可以在不创建外部类Outer对象的情况下,直接new出内部类Inner的对象。

在外部其他类中,直接访问static内部类的非静态成员

new Outer.Inner().method ();

在外部其他类中,直接访问static内部类的静态成员,即method()前也被static修饰。

new Outer.Inner.method ();

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

局部内部类

局部类和局部变量一样,是指在方法内部定义的类。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Outer  
  2. {  
  3.     int x = 3;  
  4.       
  5.     void function()    //类Outer的成员方法function()  
  6.     {  
  7.         class Inner    //成员方法中定义局部类Inner  
  8.         {  
  9.             void method()         //局部类中的成员方法method()  
  10.             {  
  11.                 System.out.println(Outer.this.x);    
  12.             }  
  13.         }  
  14.         new Inner().method();   //需生成局部类的对象,调用method()  
  15.     }  
  16. }  
  17. class InnerClassDemo7  
  18. {  
  19.     public static void main(String[] args)  
  20.     {  
  21.         Outer out = new Outer();  //生成类Outer的对象  
  22.         out.function();            
  23.     }  
  24. }  


注意:从内部类中访问局部变量,需要被声明为最终类型final

例如在类Outer的成员方法function()中增加一局部变量,应为final int y = 7

匿名内部类

匿名内部类其实就是内部类的简写,使用匿名内部类的前提条件:必须继承一个父类或实现一个接口。

不使用匿名内部类来实现抽象方法:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. abstract class Anonymous  
  2. {  
  3.     abstract void method();  
  4. }  
  5. class Outer  
  6. {  
  7.     private int x = 23;  
  8.     private class Inner extends Anonymous  
  9.     {  
  10.         void method()  
  11.         {  
  12.             System.out.println("method="+x);  
  13.         }  
  14.     }  
  15.     public void function()  
  16.     {  
  17.         new Inner().method();  
  18.     }  
  19. }  
  20. class InnerClassDemo8  
  21. {  
  22.     public static void main(String[] args)  
  23.     {  
  24.         new Outer().function();  
  25.     }  
  26. }  

如果此处的Inner类只使用一次,可以不用将其编写为一个独立的类,可写为如下代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. abstract class Anonymous{  
  2.     abstract void method();  
  3. }  
  4.   
  5. class Outer{  
  6.     private int x = 23;  
  7.     public void function(){  
  8.         new Anonymous(){  
  9.             void method(){  
  10.                 System.out.println("method="+x);  
  11.             }  
  12.         }.method();  
  13.     }  
  14. }  
  15. class InnerClassDemo10{  
  16.     public static void main(String[] args){  
  17.         new Outer().function();  
  18.     }  
  19. }  

匿名内部类的格式: new父类或者接口(){定义子类的内容}

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Outer{  
  2.     private int x = 23;  
  3.     public void function(){  
  4.         new Anonymous(){  
  5.             void method(){  
  6.                 System.out.println("method="+x);  
  7.             }  
  8.             void show(){  
  9.                 System.out.println("show");  
  10.             }  
  11.         }.method();  
  12.         new Anonymous(){  
  13.             void method(){  
  14.                 System.out.println("method="+x);  
  15.             }  
  16.             void show(){  
  17.                 System.out.println("show");  
  18.             }  
  19.         }.show();  
  20.     }  
  21. }  
  22. class InnerClassDemo10{  
  23.     public static void main(String[] args){  
  24.         new Outer().function();  
  25.     }  
  26. }  

匿名对象对方法只能调用一次,如需调用多个方法,需要建立多个匿名对象。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值