内部类总结:
?
????int?value();???
}???
??
public?interface?Destination?{???
????String?readLabel();???
}???
??
public?class?Goods?{???
????private?class?Content?implements?Contents?{???
????????private?int?i?=?11;???
????????public?int?value()?{????
????????????return?i;????
????????}???
????}???
??
????protected?class?GDestination?implements?Destination?{???
????????private?String?label;???
????????private?GDestination(String?whereTo)?{???
????????????label?=?whereTo;???
????????}???
????????public?String?readLabel()?{????
????????????return?label;????
????????}???
????}???
??
????public?Destination?dest(String?s)?{???
????????return?new?GDestination(s);???
????}???
????public?Contents?cont()?{???
????????return?new?Content();???
????}???
}???
??
class?TestGoods?{???
????public?static?void?main(String[]?args)?{???
????????Goods?p?=?new?Goods();???
????????Contents?c?=?p.cont();???
????????Destination?d?=?p.dest("Beijing");???
????}???
}???
outerObject=new?outerClass(Constructor?Parameters);?
outerClass.innerClass?innerObject=outerObject.new?InnerClass(Constructor?Parameters);?

public?class?Goods?{???
??
????private?valueRate=2;???
??
????private?class?Content?implements?Contents?{???
????????private?int?i?=?11*valueRate;???
????????public?int?value()?{????
????????????return?i;????
????????}???
????}???
??
????protected?class?GDestination?implements?Destination?{???
????????private?String?label;???
????????private?GDestination(String?whereTo)?{???
????????????label?=?whereTo;???
????????}???
????????public?String?readLabel()?{????
????????????return?label;????
????????}???
????}???
??
????public?Destination?dest(String?s)?{???
????????return?new?GDestination(s);???
????}???
????public?Contents?cont()?{???
????????return?new?Content();???
????}???
}???
静态内部类?
和普通的类一样,内部类也可以有静态的。不过和非静态内部类相比,区别就在于静态内部类没有了指向外部的引用。这实际上和C++中的嵌套类很相像了,public?class?Goods1?{???
?????public?Destination?dest(String?s)?{???
??????????class?GDestination?implements?Destination?{???
???????????????private?String?label;???
???????????????private?GDestination(String?whereTo)?{???
????????????????????label?=?whereTo;???
???????????????}???
???????????????public?String?readLabel()?{?return?label;?}???
??????????}???
??????????return?new?GDestination(s);???
?????}???
??
?????public?static?void?main(String[]?args)?{???
??????????Goods1?g=?new?Goods1();???
??????????Destination?d?=?g.dest("Beijing");???
?????}???
}??
上面就是这样一个例子。在方法dest中我们定义了一个内部类,最后由这个方法返回这个内部类的对象。如果我们在用一个内部类的时候仅需要创建它的一个对象并创给外部,就可以这样做。当然,定义在方法中的内部类可以使设计多样化,用途绝不仅仅在这一点。下面有一个更怪的例子:?
public?class?Goods2{???
?????private?void?internalTracking(boolean?b)?{???
??????????if(b)?{???
???????????????class?TrackingSlip?{???
????????????????????private?String?id;???
????????????????????TrackingSlip(String?s)?{???
?????????????????????????id?=?s;???
????????????????????}???
????????????????????String?getSlip()?{?return?id;?}???
???????????????}???
???????????????TrackingSlip?ts?=?new?TrackingSlip("slip");???
???????????????String?s?=?ts.getSlip();???
??????????}????
?????}???
??
?????public?void?track()?{?internalTracking(true);?}???
??
?????public?static?void?main(String[]?args)?{???
??????????Goods2?g=?new?Goods2();???
??????????g.track();???
?????}???
}???
你不能在if之外创建这个内部类的对象,因为这已经超出了它的作用域。不过在编译的时候,内部类TrackingSlip和其他类一样同时被编译,只不过它由它自己的作用域,超出了这个范围就无效,除此之外它和其他内部类并没有区别。?
匿名内部类?
new?interfacename(){
};?
或?new?superclassname(){
};?
public?class?Goods3?{???
?????public?Contents?cont(){???
??????????return?new?Contents(){???
???????????????private?int?i?=?11;???
???????????????public?int?value()?{????
????????????????????return?i;????
???????????????}???
??????????};???
?????}???
}???
这里方法cont()使用匿名内部类直接返回了一个实现了接口Contents的类的对象,看上去的确十分简洁。
在frame.addWindowListener(new?WindowAdapter(){???
?????public?void?windowClosing(WindowEvent?e){???
??????????System.exit(0);????
?????}???
});???
有一点需要注意的是,匿名内部类由于没有名字,所以它没有构造函数(但是如果这个匿名内部类继承了一个只含有带参数构造函数的父类,创建它的时候必须带上这些参数,并在实现的过程中使用super关键字调用相应的内容)。如果你想要初始化它的成员变量,有下面几种方法:?
如果是在一个方法的匿名内部类,可以利用这个方法传进你想要的参数,不过记住,这些参数必须被声明为final。?
将匿名内部类改造成有名字的局部内部类,这样它就可以拥有构造函数了。?
在这个匿名内部类中使用初始化代码块。?
为什么需要内部类??
public?final?class?ParseState?{
????????
..
????private?static?final?char?TAB?=?'\t';
????/**
?????*?Marker?interface?for?entries?into?the?{@link?ParseState}.
?????*/
????public?interface?Entry?{
????}
}
内部接口的实现类:
public?class?QualifierEntry?implements?ParseState.Entry?{
????private?String?typeName;

????public?QualifierEntry(String?typeName)?{
????????if?(!StringUtils.hasText(typeName))?{
????????????throw?new?IllegalArgumentException("Invalid?qualifier?type?'"?+?typeName?+?"'.");
????????}
????????this.typeName?=?typeName;
????}
????public?String?toString()?{
????????return?"Qualifier?'"?+?this.typeName?+?"'";
????}
}
这样,QualifierEntry实例就可以访问ParseState的任意成员变量。
?