总结:
创建内部类的方法:
- 在static方法中,类名必须(存疑)如此声明:OuterClassName.InnerClassName obj_inner,但在非static方法中,也可以用。
- 在static方法中,不能使用 new InnerClassName(),但是在非static方法中,可以使用new InnerClassName()
- 一言以蔽之,非static方法中比较灵活、包容,但在static方法中则比较受限。
package pkg;
public class p190 {
// .. class ..
//1st
class Contents{
private int i=11;
public int value() {return i;}
}
// 2nd
class Destination{
private String label="NUll";
public Destination(String wherego) {
label=wherego;
}
String readLable() {return label;}
}
// .. function ..
//USING class Contents and Destination as USUAL
public void ship(String desti) {
Contents c=new Contents();
Destination d=new Destination(desti);
System.out.println(d.readLable());
p190.Destination d2=new Destination("...Hongkong ");//this is allowed
System.out.println(d2.readLable());
}
//..new add function
public Contents content() {
return new Contents();
}
public Destination to(String s) {
return new Destination(s);
}
//.. main ..
public static void main(String[] args) {
//usually
p190 p=new p190();
p.ship("HongKong");
//using INNER Class
p190 p2=new p190();
p190.Contents c=p2.content();//How to create a inner Class
p190.Destination d=p2.to("ShenZhen");
System.out.println(d.readLable());
//p190.Contents c3=new Contents();//this is NOT allowed
Destination d2=p2.to("Wuhan");//why??? conflict with page 191
System.out.println(d2.readLable());
}
}