Java内部类总结.docx
《Java内部类总结.docx》由会员分享,可在线阅读,更多相关《Java内部类总结.docx(17页珍藏版)》请在冰豆网上搜索。
![Java内部类总结.docx](https://file1.bdocx.com/fileroot1/2023-2/5/c2a2f886-54a3-455b-adbc-ef999383e885/c2a2f886-54a3-455b-adbc-ef999383e8851.gif)
Java内部类总结
Java内部类总结
Java内部类总结
Java内部类其实在J2EE编程中使用较少,不过在窗口应用编程中特别常见,主要用来事件的处理。
其实,做非GUI编程,内部类完全可以不用。
内部类的声明、访问控制等于外部类有所不同,要灵活使用内部类来编写程序,还是有相当难度的,Java发明了这种难懂的玩意儿,在其他语言中是没有的,但是在Java中,内部类也相当的重要,尤其做GUI开发时候,事件的响应处理全靠内部类了。
内部类所做的功能使用外部类也同样可以实现,只是有时候内部类做的更巧妙些。
内部类按照其所在位置不同,可分为以下几种:
1、(普通的)内部类(最常见的内部类,内部类的定义与类成员平级,)
2、方法内部类
3、匿名类
4、静态内部类
5、接口内部类
一、内部类声明与访问
1、内部类直接在类的内部进行声明。
可以声明为private、protected、public或者默认访问权限,这个访问权限约定和外部类完全一样。
2、内部类自动拥有对其外围类所有成员(方法、属性)的访问权。
如果内部类和外部类成员的名字完全相同,在内部类方法中要访问外部类成员,则需要使用下面的方式来访问:
外部类名.this.外部成员名,例如Outer.this.i++;(看例子)
3、必须使用外部类对象来创建内部类对象,而不是直接去new一个。
格式为:
外部对象名.new内部类构造方法
比如要创建一个内部类iner对象,需要这么做:
Outerouter=newOuter();
Outer.Inneriner=outer.newInner();
/**
*内部类创建与初始化
*
*@authorleizhimin2009-7-1713:
51:
52
*/
publicclassOuter{
privateinti=10;
privateinty=8;
Outer(){
System.out.println("调用Outer构造方法:
outer");
}
publicvoidsayMsg(){
System.out.println("Outerclass!
");
}
classInner{
inti=1000;
Inner(){
System.out.println("调用Inner构造方法:
inner");
}
voidinnerMsg(){
System.out.println(">>>>>Innerclass!
");
sayMsg();
//访问内部类自己的成员i,也可以写成this.i++
this.i++;
//访问外部类的成员i和y
Outer.this.i++;
y--;
}
intgetI(){
returni;
}
}
publicvoidtest(){
Innerin=newInner();
in.innerMsg();
}
publicintgetI(){
returni;
}
publicvoidsetI(inti){
this.i=i;
}
}
classTest1{
publicstaticvoidmain(String[]args){
Outerouter=newOuter();
outer.test();
System.out.println(outer.getI());
System.out.println("-------1--------");
Outer.Inneriner=outer.newInner();
iner.innerMsg();
System.out.println(iner.getI());
System.out.println("-------2--------");
System.out.println(outer.getI());
}
}
运行结果:
调用Outer构造方法:
outer
调用Inner构造方法:
inner
>>>>>Innerclass!
Outerclass!
11
-------1--------
调用Inner构造方法:
inner
>>>>>Innerclass!
Outerclass!
1001
-------2--------
12
Processfinishedwithexitcode0
二、内部类与接口
1、内部类可以实现接口。
2、内部类之间相互可见,但并非内部类之间方法都可见。
publicinterfaceFoo{
voidsay();
}
publicinterfaceBar{
voidreadme();
}
/**
*内部类实现接口
*
*@authorleizhimin2009-7-1714:
57:
50
*/
publicclassTest2{
publicstaticvoidmain(String[]args){
Outerouter=newOuter();
Foof=outer.genFoo();
Barb=outer.genBar();
f.say();
b.readme();
}
}
classOuter{
privateclassFooImplimplementsFoo{
publicvoidsay(){
System.out.println("sayfoo!
");
}
}
privateclassBarImplimplementsBar{
publicvoidreadme(){
System.out.println("saybar!
");
}
}
publicFoogenFoo(){
returnnewFooImpl();
}
publicBargenBar(){
returnnewBarImpl();
}
}
输入结果:
sayfoo!
saybar!
Processfinishedwithexitcode0
三、访问权限
外部类分两种:
一种嵌入了内部类声明代码外部类,称为直接外部类。
另一种是与内部类没有任何关系的外部类,称为外部类。
在同一个直接外部类中,内部类之间所有的方法都是相互可见的,包含在直接外部类的main()中可见。
在外部类中,要看到一个类的内部类成员,则至少要求这个内部类的class和成员权限大于或等于protected。
/**
*内部类实现接口
*
*@authorleizhimin2009-7-1714:
57:
50
*/
publicclassTest2{
publicstaticvoidmain(String[]args){
Outero=newOuter();
Outer.Barb=o.genBar();
b.readme();
}
}
classOuter{
protectedclassFoo{
protectedvoidsay(){
System.out.println("sayfoo!
");
}
privatevoidtest(){
System.out.println("----test------");
}
}
protectedclassBar{
protectedvoidreadme(){
System.out.println("saybar!
");
newFoo().test();
}
}
publicFoogenFoo(){
returnnewFoo();
}
publicBargenBar(){
returnnewBar();
}
}
四、方法内部类
方法内部类只在该方法内部可见,方法内部类可以定义在方法中的任何位置。
/**
*内部类实现接口
*
*@authorleizhimin2009-7-1714:
57:
50
*/
publicclassTest2{
publicstaticvoidmain(String[]args){
Outerouter=newOuter();
Foof=outer.genFoo();
Barb=outer.genBar();
f.say();
b.readme();
}
}
classOuter{
publicFoogenFoo(){
//方法内的内部类
classFooImplimplementsFoo{
publicvoidsay(){
System.out.println("sayfoo!
");
}
}
returnnewFooImpl();
}
publicBargenBar(){
Barb=null;
if(true){
//任意位置的内部类
classBarImplimplementsBar{
publicvoidreadme(){
System.out.println("saybar!
");
}
}
b=newBarImpl();
}
returnb;
}
}
运行结果:
sayfoo!
saybar!
Processfinishedwithexitcode0
五、匿名类
匿名类不给出类名,直接定义一个类,通常这个类实现了某种接口或者抽象。
匿名类的访问权限更没有讨论价值了,看个例子就行了。
在一些多线程程序中比较常见,有点变态,呵呵。
/**
*匿名类.
*
*@authorleizhimin2009-7-1715:
56:
17
*/
publicclassTest3{
publicFoof=newFoo(){
publicvoidsay(){
System.out.println("O(∩_∩)O哈哈~!
");
}
};
publicFootest(){
returnnewFoo(){
publicvoidsay(){
System.out.println("sayfoo!
");
}
};
}
publicstaticvoidmain(String[]args){
Test3t=newTest3();
t.f.say();
t.test().say();
}
}
interfaceFoo{
voidsay();
}
运行结果:
O(∩_∩)O哈哈~!
sayfoo!
Processfinishedwithexitcode0
/**
*普通类的匿名初始化
*
*@authorleizhimin2009-7-1716:
13:
31
*/
publicclassFk{
privateStringx;
publicFk(Stringx){
this.x=x;
}
@Override
publicStringtoString(){
return"Fk{"+
"x='"+x+'\''+
'}';
}
}
classTest4{
publicFkhehe(){
//把后面的一对大括号去掉呢,呵呵
returnnewFk("fk"){
};
}
publicstaticvoidmain(String[]args){
Test4t=newTest4();
Fkf=t.hehe();
System.out.println(f);
}
}
运行结果:
Fk{x='fk'}
Processfinishedwithexitcode0
还有一个不得不提的经典实例,来自thininginjava,有改动:
interfaceService{
voidmethod1();
voidmethod2();
}
interfaceServiceFactory{
ServicegetService();
}
classImplementation1implementsService{
privateImplementation1(){}
publicvoidmethod1(){System.out.println("Implementation1method1");}
publicvoidmethod2(){System.out.println("Implementation1method2");}
publicstaticServiceFactoryfactory=newServiceFactory(){
publicServicegetService(){
returnnewImplementation1();
}
};
}
classImplementation2implementsService{
privateImplementation2(){}
publicvoidmethod1(){System.out.println("Implementation2method1");}
publicvoidmethod2(){System.out.println("Implementation2method2");}
publicstaticServiceFactoryfactory=newServiceFactory(){
publicServicegetService(){
returnnewImplementation2();
}
};
}
publicclassFactories{
publicstaticvoidserviceConsumer(ServiceFactoryfact){
Services=fact.getService();
s.method1();
s.method2();
}
publicstaticvoidmain(String[]args){
serviceConsumer(Implementation1.factory);
serviceConsumer(Implementation2.factory);
}
}
这个应用给了我们很多思考,我就不说了,不同人看了会有不同的感受。
内部类的巧妙使用会让你的代码很牛,如果要形容下,那就是:
没看懂的时候感觉神出鬼没,看懂后感觉鬼斧神工。
不过这些代码多了,别人想看懂都难,想看懂你思路就难上加难了。
呵呵!
六、静态内部类
静态内部类是staticclass型的内部类,这种内部类特点是:
它不能访问外部类的非静态成员。
要创建静态内部类对象时候,也不需要外部类对象了,直接可以:
new外部类名.内部类构造方法
来创建,给个例子:
/**
*静态内部类
*
*@authorleizhimin2009-7-1716:
53:
05
*/
publicclassOuter{
publicstaticinti=500;
protectedstaticclassInner{
inti=100;
Stringname;
Inner(Stringname){
this.name=name;
}
voidsayHello(){
System.out.println("Hello"+name);
Outer.i++;
}
}
publicInnergenInner(Stringname){
returnnewInner(name);
}
}
classTest{
publicstaticvoidmain(String[]args){
Outer.Innerin1=newOuter.Inner("1111");
in1.sayHello();
System.out.println(Outer.i);
Outer.Innerin2=newOuter().genInner("2222");
in2.sayHello();
System.out.println(Outer.i);
}
}
运行结果:
Hello1111
501
Hello2222
502
Processfinishedwithexitcode0
七、接口内部类
接口内部类自动都是publicstatic的,相当于为接口定义了一种变量类型,这在java的设计中就有使用,比如在HashMap中,就有:
staticclassEntryimplementsMap.Entry
下面我给个例子,
/**
*接口内部类
*
*@authorleizhimin2009-7-1717:
20:
28
*/
publicinterfaceAInterface{
voidreadme();
classInner1implementsAInterface{
publicvoidreadme(){
System.out.println("我是一个接口内部类");
}
}
}
classMain{
publicstaticvoidmain(String[]args){
AInterface.Inner1in1=newAInterface.Inner1();
in1.readme();
}
}
八、内部的类的嵌套
所谓内部类嵌套,就是内部类里面再定义内部类。
其实这种用法还真没见过,试试写个简单例子看看吧:
/**
*嵌套内部类
*
*@authorleizhimin2009-7-1717:
33:
48
*/
publicclassOuter{
privatevoidf0(){
System.out.println("f0");
}
classA{
privatevoida(){
f0();
System.out.println("a");
}
classB{
protectedvoidb(){
a();
System.out.println("b");
}
}
}
}
classTest{
publicstaticvoidmain(String[]args){
Outero=newOuter();
Outer.Aa=o.newA();
Outer.A.Bb=a.newB();
b.b();
}
}
运行结果:
f0
a
b
Processfinishedwithexitcode0
八、内部类的继承
内部类的继承,可以继承内部类,也可以继承外部类。
/**
*内部类的继承,可以继承内部类,也可以继承外部类
*
*@authorleizhimin2009-7-2213:
50:
01
*/
publicclassOuter{
classInner{
voiddoSomething(){
System.out.println("Innerdoing...");
}
}
classInner2extendsInner{
voiddoSomething(){
System.out.println("Inner2doing...");
}
voidreadme(){
System.out.println("HeHe!
");
}
}
}
classTest{
publicstaticvoidmain(String[]args){
Outerouter=newOuter();
Outer.Innerin=outer.newInner();
Outer.Inner2in2=outer.newInner2();
in.doSomething();
in2.doSomething();
in2.readme();
}
}
运行结果:
Innerdoing...
Inner2doing...
HeHe!
Processfinishedwithexitcode0
总结:
内部类是Java中最复杂深奥的概念之一,而且内部类在访问控制,修饰符,继承,实现,抽象,序列化等等很多方面都是一个很让人迷惑的问题,在实际中,这些问题也许永远没机会没时间搞清,但是一般说来,懂得以上的内部类的知识就足够用了。
内部类的设计也许是弥补Java语言本身的先天不足吧,作为语言来说,这个特性太变态了点,难道就没别的法了?
以上的总结完全是建立在实践基础上的,所列举的例子也许偏颇,不能全面反映问题的本质,希望有兴趣的博友多多发表自己的看法与观点。