氟塑料离心泵www.buybeng.com

jquery教程http://www.software8.co/wzjs/jquery/

理解 java 内部类

1、内部类基础知识:

一般定义在java类内部的类成为内部类

内部类可以分为:定义在方法体外部的类、定义方法内部的类、静态内部类(只能定义在方法外部),匿名内部类

说明:

定义在方法外面的类:

类的成员变量(静态、非静态)可以访问,为了保证能够正确的引用的类的成员变量,所以必须先实例化外部类的对象,才可以实例化内部类的对象

访问权限可以任何,可以把它看成类的成员变量,这样理解就好多来了。

定义在方法体内的类;

类的成员变量(静态、非静态)可以访问,为了保证能够正确的引用的类的成员变量,所以必须先实例化外部类的对象,才可以实例化内部类的对象

访问权限不可以有,把他看成方法的局部变量就可以了。

静态内部类:

只能访问类的静态成员变量

访问权限任何

匿名内部类:

类的成员变量(静态、非静态)可以访问,为了保证能够正确的引用的类的成员变量,所以必须先实例化外部类的对象,才可以实例化内部类的对象

访问权限不可以有

2、内部类的作用

内部类可以很好的隐藏类,一般类不允许有private protect default访问权限。

内部类可以实现多重基础,弥补了java不能多继承的特点

3、例子

  1. package com.ajun.test.innerclass.example;  
  2.   
  3. /** 
  4.  * 水果内容 
  5.  * @author Administrator 
  6.  * 
  7.  */  
  8. public interface Contents {  
  9.      String value();  
  10. }  

  1. package com.ajun.test.innerclass.example;  
  2.   
  3. /** 
  4.  * 水果目的地 
  5.  * @author Administrator 
  6.  * 
  7.  */  
  8. public interface Destination {  
  9.   
  10.     //目的地  
  11.     String readLabel();  
  12. }  

  1. package com.ajun.test.innerclass.example;  
  2.   
  3. public class Goods {  
  4.   
  5.     private String des="is ruit!!";  
  6.       
  7.     //方法外部  
  8.     private class Content implements Contents{  
  9.         private String name = "apple "+des;  
  10.         @Override  
  11.         public String value() {  
  12.             return name;  
  13.         }  
  14.     }  
  15.       
  16.     //方法外部  
  17.     private class GDestination implements Destination{  
  18.         private String label ;  
  19.         private GDestination(String label){  
  20.             this.label= label;  
  21.         }  
  22.         @Override  
  23.         public String readLabel() {  
  24.             return label;  
  25.         }  
  26.     }  
  27.       
  28.       
  29.     //匿名内部类  
  30.     public Destination getdestination(final String label){  
  31.         return new Destination(){  
  32.             @Override  
  33.             public String readLabel() {  
  34.                 return label;  
  35.             }  
  36.         };  
  37.     }  
  38.       
  39.     public Destination dest(String s){  
  40.         return new GDestination(s);  
  41.     }  
  42.       
  43.     public Contents  content(){  
  44.         return new Content();  
  45.     }  
  46.       
  47.     public Destination dest2(String s){  
  48.         class GDestination implements Destination{  
  49.                 private String label;  
  50.                 private GDestination(String label){  
  51.                     this.label= label;  
  52.                 }  
  53.                 @Override  
  54.                 public String readLabel() {  
  55.                     return label;  
  56.                 }  
  57.         }  
  58.         return new GDestination(s);  
  59.    }  
  60.       
  61. }  

  1. package com.ajun.test.innerclass.example;  
  2.   
  3. public class Test {  
  4.   
  5.     public static void main(String [] a){  
  6.         Goods gs = new Goods();  
  7.         Contents c = gs.content();  
  8.         Destination d = gs.dest("Beijing");  
  9.         System.out.println(c.value());  
  10.         System.out.println(d.readLabel());  
  11.         Destination d1 = gs.getdestination("Shanghai");  
  12.         System.out.println(d1.readLabel());  
  13.         System.out.println(gs.dest2("Tianjin").readLabel());  
  14.     }  
  15. }  

其中Content和Gdestination得到了很好的隐藏,外面调用的时候,根本就不知道调用的是具体哪个类,使这个类拥有多继承的特性。



输出;

  1. apple is ruit!!  
  2. Beijing  
  3. Shanghai  
  4. Tianjin  

posted on 2012-10-27 08:53 你爸是李刚 阅读(1253) 评论(0)  编辑  收藏


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


网站导航:
 
<2012年10月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿

随笔档案

文章档案

技术网站

行业网站

搜索

最新评论

阅读排行榜

评论排行榜

站长网 氟塑料离心泵 注塑机 液晶广告机