java学习面向对象之内部类

什么是面向对象内部类呢?所谓的内部类,即从字面意义上来理解的话,就是把类放到类当中。

那么内部类都有什么特点呢?

1、内部类可以访问包裹他的类的成员。

2、如果包裹他的类想访问被其包裹的类的话就得实例化,才可以访问。

代码示例:

 1 class Outer
 2 {
 3     
 4     int num = 10;
 5 
 6     void show()
 7     {
 8 
 9         System.out.println("Outer Show method!!");
10 
11     }
12 
13     void method()
14     {
15 
16         Inner a  = new Inner();
17         a.innerShow();
18 
19     }
20 
21 
22     static class Inner
23     {
24 
25         static final int num = 20;
26 
27         void innerShow()
28         {
29 
30             System.out.println("The num is "+num);
31             //show();
32             staticShow();
33 
34         }
35 
36         static void staticShow()
37         {
38 
39             System.out.println("Static Inner Show");
40 
41         }
42 
43     }
44 
45 }
46 
47 class Inner
48 {
49     
50 
51     void innerShow()
52     {
53 
54         System.out.println("The num is ");
55 
56     }
57 
58 }
59 
60 class InnerClassDemo1
61 {
62     
63 
64     public static void main(String[] args) {
65         
66         Outer.Inner out = new Outer.Inner();
67         out.innerShow();
68 
69     }
70 
71 }

内部类的作用:

  一般用来作为类的设计。

  当我们描述事物的时候,发现其中还有事物,并且这个事物还在访问被描述事物的内容。这个时候就用到了内部类。

 内部类还有哪些特点呢?

1、内部类中有静态变量的话,内部类必须是静态的。也就是说内部类可以被修饰符修饰,因为他本身也是类内部的成员。

2、如果想从外部直接调用内部类,必须按照这种方式来调用:

 1 class InnerClassDemo1
 2 {
 3 
 4     public static void main(String[] args) {
 5         
 6         Outer.Inner inner = new Outer().new Inner();
 7         inner.showInner();
 8 
 9     }
10 
11 }

内部类细节注意:

1、this变量,我们知道一个类对象被new之后肯定有一个this与之相关联,那么类当中有内部类的时候,如果单凭一个this的话可能就会造成混淆。我们来写段代码来测试一下:

 1 class Outer
 2 {
 3 
 4     private int num = 10;
 5 
 6     class Inner
 7     {
 8         int num = 15;
 9         void showInner()
10         {
11 
12             System.out.println("The Outer num is " + this.num);
13             System.out.println("The Inner num is " + Inner.this.num);
14             system.out.println("The Oute num is "+Outer.this.num);
15 
16         }
17 
18     }
19 
20     void method()
21     {
22 
23         Inner inner = new Inner();
24         inner.showInner();
25 
26     }
27 
28 }
29 
30 class InnerClassDemo1
31 {
32 
33     public static void main(String[] args) {
34         
35         Outer.Inner inner = new Outer().new Inner();
36         inner.showInner();
37 
38     }
39 
40 }

 

2、为什么内部类能够直接访问外部类的成员呢,因为内部类持有外部类的引用,书写格式就是外部类.this.

 

内部类的另外一个特点就是,可以放在局部位置上,什么是局部位置呢,形象一点来说的话,就是可以放到函数当中。示例:

 1 class Outer
 2 {
 3 
 4     private int num = 10;
 5 
 6     void method()
 7     {
 8      int num = 9;//必须用final关键字修饰之后,在Inner但中才可以访问这个值。
 9         class Inner
10         {
11             //int x = 9;
12             void show()
13             {
14 
15                 System.out.println("the num is :"+num);
16 
17             }
18 
19         }
20 
21         Inner inner = new Inner();
22         inner.show();
23 
24     }
25 
26 }
27 
28 class InnerClassDemo1
29 {
30 
31     public static void main(String[] args) {
32         
33         Outer out = new Outer();
34         out.method();
35 
36     }
37 
38 }

内部类在局部位置上只能,访问局部但中由final关键字修饰的局部变量。为什么呢?因为当类在局部的时候,当局部方法进栈之后,变量生成,出栈之后变量消失,如果在方法外调用局部作用域的话,因为方法已经出栈了,那么局部变量已经消失了,释放了。此时若要保证外部能够访问局部位置上的内部类中的调用了局部位置的变量的方法,只能够把局部位置上的局部变量用final修饰成常量,这样才可以供外部访问。java是一门严谨性的语言。

posted @ 2013-10-06 12:31  stark_javac  阅读(211)  评论(0编辑  收藏  举报