public class Parcel {
class Contents{
private int i =1;
public int value() {
return i;
}
}
class Destination{
private String labelString;
Destination(String whereTo){
labelString =whereTo;
}
String readLabel(){return labelString;}
}
public Destination to(String string){
//在outer class的non-static方法可直接产生inner class类对象
return new Destination(string);
}
public Contents contents() {
return new Contents();
}
public void ship(String dest) {
//在outer class的nouter class中可直接通过InnerClassName来指定对象型别
Contents contents = contents();
Destination destination =to(dest);
System.out.println(destination.readLabel());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Parcel parcel1 = new Parcel();
parcel1.ship("11111");
Parcel parcel2 = new Parcel();
//在outer class的非non-static函数内产生inner class对象
Parcel.Contents contents = parcel2.contents();
Parcel.Destination destination = parcel2.to("2222");
//不能在static函数直接生成inner class对象
// Contents contents2 = new Contents();
}
测试
最新推荐文章于 2024-09-27 20:40:55 发布