如何構(gòu)建內(nèi)部類
//外部類HelloWorld
public class HelloWorld {
? ? ? ??
? ? // 內(nèi)部類Inner,類Inner在類HelloWorld的內(nèi)部
? ? public class Inner {
? ? ? ??
// 內(nèi)部類的方法
public void show() {
System.out.println("welcome to imooc!");
}
}
? ? ?
public static void main(String[] args) {
? ? ? ??
? ? ? ? // 創(chuàng)建外部類對象
HelloWorld hello = new HelloWorld();
? ? ? ? // 創(chuàng)建內(nèi)部類對象
Inner i = hello.new Inner();
? ? ? ? // 調(diào)用內(nèi)部類對象的方法
i.show();
}
}
2016-07-30
public class Outer{ //構(gòu)建外部類
????public class Inner{ //構(gòu)建內(nèi)部類
????????public void show(){
????????????????System.out.println( "我是Outer的內(nèi)部類中的方法." ); ??
????????}
? ? ?}
???? public static void main( String[] args ){
????????????Outer one = new Outer(); //創(chuàng)建外部類對象
????????????Inner instan = one.new Inner(); //創(chuàng)建內(nèi)部類對象
????????????instan.show(); //調(diào)用內(nèi)部類對象的方法
????}
}