Java: 内部类

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

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

 1 class Outer
 2 {
 3     int x = 3;
 4 
 5     void method()
 6     {
 7         Inner in = new Inner();
 8         in.function();
 9     }
10 
11     class Inner//内部类
12     {
13         int x = 4;
14         void function()
15         {
16             int x = 6;
17             System.out.println("Inner class:" + x);//x=6
18             System.out.println("Inner class:" + this.x);//x=4
19             System.out.println("Inner class:" + Outer.this.x);//x=3
20         }
21     }
22 }
23 class InnerClassDemo
24 {
25     public static void main(String[] args) 
26     {
27         
28         Outer out = new Outer();
29         out.method();
30 
31         //直接访问内部类的成员
32         Outer.Inner in = new Outer().new Inner();
33         in.function();
34     }
35 }

 

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

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

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

注意:
  当内部类中的定义了静态成员,该内部类必须是静态的

  当外部类中的静态方法访问内部类时,内部类也必须是静态的

 1 class Outer
 2 {
 3     private static int x = 3;
 4 
 5     static class Inner//静态内部类
 6     {
 7         void function()
 8         {
 9             System.out.println("Inner class:" + x);
10         }
11     }
12 }
13 class InnerClassDemo
14 {
15     public static void main(String[] args) 
16     {
17         new Outer.Inner().function();
18     }
19 }

 内部类的使用:

  当描述事务时,如果该事务内部还有事物,该事务用内部类来描述

class Body
{
    ...
    private class Heart
    {
        ...
    }
}

内部类定义在局部时:
  1,不可以被成员修饰符修饰
  2,可以直接访问外部类的成员,因为还持有外部类的引用;但是不可以访问他所在的局部中的变量,只能访问被final修饰的局部变量。

 1 class Outer
 2 {
 3     private static int x = 3;
 4     
 5     void method()
 6     {
 7         final int y = 4;
 8         class Inner
 9         {
10             void function()
11             {
12                 System.out.println(Outer.this.x);
13                 System.out.println(y);
14             }
15         }
16         new Inner().function();
17     }
18 }
19 class InnerClassDemo
20 {
21     public static void main(String[] args) 
22     {
23         new Outer().method();
24     }
25 }

匿名内部类:
  1,匿名内部类其实就是内部类的简写格式;
  2,定义匿名内部类的前提:内部类必须是继承了一个类或者实现了一个接口
  3, 匿名内部类的各式: new 父类或者接口(){定义子类的内容}
  4,其实匿名内部类就是一个匿名子类对象,可以理解为带内容的对象

 1 abstract class Abs
 2 {
 3     abstract void show();
 4 }
 5 class Outer
 6 {
 7     private int x = 3;
 8     
 9     /*
10     class Inner extends Abs
11     {
12         void show()
13         {
14             System.out.println("Method:"+x);
15         }
16     }
17     */
18     public void function()
19     {
20         
21         new Abs()//匿名内部类
22         {
23             void show()
24             {
25                 System.out.println("x="+x);
26             }
27         }.show();
28     }
29 }
30 class InnerClassDemo
31 {
32     public static void main(String[] args) 
33     {
34         new Outer().function();
35     }
36 }

 

posted @ 2015-03-01 21:32  司英成·Owen  阅读(128)  评论(0)    收藏  举报