首 页枣庄华育行业资讯课程介绍师资力量百问百答就业广场荣誉展示学员作品在线报名
beach wedding dresses Wedding dresses, bridal advice, wedding planning tips, flowers ...
Features planning guides and a wedding budget planning tool. Includes a large gallery of bridal fashion and a directory of wedding services.
oil painting Flower oil paintings Bedroom Furniture Gumtree Eastern Suburbs
Flower oil paintings,bedroom, Gumtree Classifieds.
mbt shoes Over 50% off all cheap MBT shoes free worldwide shipping
MBT shoes under sales promotion, all the products are sold nearly at its cost and free shipping. It is really a good chance for shopping. just grasp the ...
uggs on sale Mens ugg boots,mens uggs,ugg boots men,ugg mens slippers,ugg for ...
Mens ugg boots, mens-uggboots.com. This online store to find the best cheap deals on Mens ugg boots,mens uggs.Cheap & Safe & Fast & Reliable all in this ...
robe de mariee Magasin de robe de mariee, liens utiles et gratuits sur magasin de ...
Pour apporter des prcisions sur magasin de robe de mariee, vous pouvez consulter la rubrique Robe de marie qui, entre autres sujets, aborde les thmes de ...
plus size lingerie achat kamas, Acheter des Kamas, dofus kamas, kamas, dofus kama
Welcome to our Acheter Kamas store. We are specialized, professional and reliable website for Acheter des Kamas selling. By the same token, we furnish the ...
pasties Vestido de Noiva Estilo Romntico - Fotos e Famosas | Casamento
Os vestidos de casamento no estilo romntico so lindos e so feitos com bastante renda, apresentando uma aparncia mais leve e delicada.
120 402 455 485 988 1415 1421 1487 1819 1990 2069 2544 2689 2958 2974 3176 3190 3241 3726 4007 4119 4268 4436 4465 4550 4672 4696 4779 4907 5124
 您现在的位置: 鲁南IT教育网|枣庄华育国际|枣庄软件培训|山东华育|枣庄培训|软件工程师 >> 在线报名 >> 正文
文章属性:[热门]
JAVA基础:Java的内层类和外层类
[ 作者:未知 转贴自:未知 点击数:905 文章录入:admin 更新时间:2007-12-10 10:21:14 ]
什么是inner class?
内层类就是在另一个类的内部定义的一个类,定义这个类的类被称作外层类.内层类最大
的优点
在于它拥有对在外层类中定义的所有变量和方法的访问权.这也是内层类非常适合于创建
适配
器的原因;内层类的另一个优点是用户可以看到它们实例化一个接口的实例,如下代码(这
是按
钮单击事件的代码):
//outer class is "Frame1"
button1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
button1_actionPerformed(e); //button1_actionPerformed(e)方法定义在Fr
ame1中
}
});
其中:
java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
button1_actionPerformed(e);
}
}
这就是定义的一个内层类!事实上ActionListener是一个接口,其后的{}中的内容是这个
内层
类的唯一的一个方法:public void actionPerformed(ActionEvent e);这个内层类没有
任何
属性.这个方法中的唯一动作就是调用了按钮单击的方法----button1_actionPerformed
(e)
----这个方法在外层类Frame1中定义:我们的处理就在这个方法中,比如我们可以写:
void button1_actionPerformed(ActionEvent e){
System.exit(0);
}
在这里,我们看到了内层类在调用外层类的方法----button1_actionPerformed(e).同时
,我们
注意到该内层类定义的前面的"new",她把这个内层类实例化了!
匿名类和命名类
在上面这段代码中,定义的内层类没有类名,这是因为这个类只被使用了一次,是一个"一
次性"
类,即匿名类.不用担心这个类的名字,Java会在背后帮我们做这些.在编译之后,可以在项
目目
录下找到这些类,它们具有这样的名字:Frame1$1.class,Frame1$2.class等等,以此类推
.
我们也可以使用这项技术如:
Button newButton = new Button(){
public void someNewMethod(){
/* Defination of extra method here */
}
};
这里,newButton对象是正常的java.awt.Button,其中含有为它定义的一个额外方法.
当想在一个内层类内创建多个对象时,要使用命名类,而不要使用匿名类.实际上命名类在
本质
上和匿名类没有什么不同,其代码如下:
public Frame1 extends DecoratedFrame{
...
public Frame1(){
...
try{
jbInit();
}
catch(Exception e){
e.printStackTrace();
}
} //end of Frame1 constructure
private void jbInit() throws Exception{
...
button1.addActionListener(new Button1Adapter());
...
} //end of jbInit() method
class Button1Adapter implements java.awt.event.ActionListener{
public void actionPerformed(ActionEvent e){
button1.actionPerformed(e);
}
}
void button1_actionPerformed(ActionEvent e){
/* Your own statements */
}
}
这里,为名为Button1Adapter的一个类提供了正常的定义.这个类实现ActionListener接
口.
new创建适配器的一个实例,可以将其余button1对象注册到一起.内层类将会拥有这样的
文件
名:Frame1$Button1Adapter.class.
上面的代码也可以不用内层类来实现.但标准的方法是应该使用内层类.各位如果感兴趣
的话,
也可以试试不用内层类来实现上述代码...:-)
打印此文】 【关闭窗口

版权所有 Copyright? 2003 枣庄华育国际 页面执行时间:686.5234毫秒
地址Add:枣庄市市中区华山路90号(新三中往北50米路西) 银狐大厦对过  企业邮箱E-mail:tzwq@sina.com
电话Tel:0632-3816111 3816222 3816333  0632-3201039
E-mail:tzwq@sina.com
建议使用Microsoft Internet Explorer 5.0 以上版本浏览器 800×600分辨率浏览本站  
鲁ICP备08007477号