Java-黑蝙蝠  
My name is mozart0
外表涩苦但留醇香于舌尖
online
Adult Dating
公告
  • 多一份自信,多一份成功!
    多看一本书,多一份机遇!
    多一次微笑,多一份快乐!
日历
<2006年10月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234
统计
  • 随笔 - 10
  • 文章 - 0
  • 评论 - 32
  • 引用 - 0

导航

常用链接

留言簿(6)

随笔分类(10)

随笔档案(10)

相册

天气预报

常去论坛

搜索

  •  

积分与排名

  • 积分 - 37719
  • 排名 - 1193

最新评论

阅读排行榜

评论排行榜

 
 1 /*
 2  * 
 3     内部类的种类,可细分为四种: 
 4  1 类级别的 即用static修饰的内部类
 5  2 非static  不用static修饰的内部类
 6  3 写在方面里面的内部类
 7  4 匿名内部类(常用在事件监听里)
 8    下面我们通过例子来认识Inner Class
           先介绍前2种 即1 和 2
         Author:blackbat
 9    2006-10-3 2:04
10  *
11   */

12
13 class  ImOutClass {
14       private   static  String staticAtt  =   " 外部类的类变量 " ;
15       private   String instAtt  =   " 外部类的实例变量 " ;
16     
17      //  private static String xx = "外部静态变量";    
18      //  private  String xx = "外部类的实例变量";
19
20      // 外部类的非静态方法
21      public   void  instanMethod() {
22         System.out.println( " 和外部类实例方法 " );
23     }

24
25      // 外部类的静态方法
26      public   static   void  staticMethod() {
27         System.out.println( " 和外部类静态方法 " );
28     }

29
30      // 静态内部类(static Inner Class)
31      public   static   class   StaticInnerClass {
32         
33          public  StaticInnerClass() {
34             System.out.println( " 我是静态内部类 " );
35         }

36
37          public   void  access() {
38             System.out.println( " 我可以访问 " + staticAtt);
39             staticMethod();
40             // TestIt.java:40: non-static method instanMethod() cannot be referenced from a static context
41            // instanMethod();
42         }

43
44     }

45     
46      // 非静态内部类(non-static Inner Class)
47      public   class  InstInnerClass {
48         
49          public   void  InstInnerClass() {
50             System.out.println( " 我是成员级内部类,非静态内部类 " );
51         }

52
53          public   void  access() {
54             System.out.println( " 我可以访问 " + instAtt);
55             instanMethod();
56             staticMethod();
57         }

58
59     }

60
61  }
		
				
下面是测试类,即如何访问刚才所建立的内部类。

  public   class  TestIt {

     
public   static   void  main(String blackbat[]) {
         
// 建立静态内部类(static Inner Class)的对象
         ImOutClass.StaticInnerClass staticIC  =   new  ImOutClass.StaticInnerClass();
         staticIC.access();

         
/*  建立非静态内部类(non-static Inner Class)的对象
          *  注意这种建立对象的格式 首先创建外部类的对象
          *  然后使用对象.new 来创建。
          
*/

         ImOutClass  outC 
=   new  ImOutClass();
         ImOutClass.InstInnerClass inC
=  outC. new  InstInnerClass();
         inC.access();
     }


 }

 

  
上面我们认识了前2中类型的内部类,下面我们来认识后2种类型的内部类。


import javax.swing.JFrame;
import java.awt.event.*;


class  InnerClassTest2 extends JFrame{

    
public static void main(String[] args) {
        InnerClassTest2  out 
= new InnerClassTest2();
        Object obj 
= out.inSomeMethod();
        System.out.print(obj);
//实际调用了obj.toString();

        JFrame frame 
= new JFrame("Anonymous Inner Class Example");
        
        
//匿名内部类,注意其写法。(Anonymous Inner Class)
        frame.addWindowListener(new WindowAdapter(){
            
public void windowClosing(WindowEvent e){
                System.exit(
0);
            }

        }
);//这里以分号结束。

        frame.setVisible(
true);
        frame.setSize(
200,300);
    }


    
public Object inSomeMethod(){
        
        
//方法里的内部类(Method Inner Class)
        class methodInnerClass{
            
public String toString(){
                
return "my nickname is white";
            }

        }


        
return new methodInnerClass();//返回该内部类对象
    }


}



最近正在备战scjp,复习到内部类这章故整理一下,加深印象。:)
ok , 到此为止。 以上代码 xp+jdk5.0测试通过。


posted on 2006-10-04 02:04 黑蝙蝠 阅读(792) 评论(1)  编辑  收藏 所属分类: J2SE(基础)
评论:
  • # re: 搞定Inner Class(内部类)  久城 Posted @ 2006-10-12 14:13
    不错,学习中...
    PS:写在方法里的内部类主要需要注意,方法里的内部类不能访问该方法中的局部变量,但能访问外类的成员变量..  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 黑蝙蝠 Powered by: 博客园 模板提供:沪江博客