為什么這樣不行?用對(duì)象外部類調(diào)用外部類方法,結(jié)果無(wú)法執(zhí)行到內(nèi)部類方法?
ublic class HelloWorld?
{
? ? // 外部類中的show方法
? ? public void show()?
? ? {?
// 定義方法內(nèi)部類
? ?final int a=25;
? ? ? ? int b=13;
? ? ? ? class MInner
? ? ? ? {
? ? ? ? ? ? int c=2;
? ? ? ? ? ? public void print()
? ? ? ? ? ? {
? ? ? ? ? ? ?System.out.println("訪問a:"+a);
? ? ? ? ? ? ?System.out.println("訪問c:"+c);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public static void main(String[] args)
? ? {
? ? ? ? HelloWorld mo=new HelloWorld();
? ? ? ? mo.show();
? ? }
}
2016-05-04
因?yàn)槟阍趕how()方法中沒有調(diào)用內(nèi)部類的print()方法;
你可以在show()中生成一個(gè)內(nèi)部類對(duì)象,用這個(gè)對(duì)象調(diào)用print()方法;
如:
MInner mi = new MInner();
? ? ? ? mi.print();
2016-05-04
因?yàn)槟氵@樣子調(diào)用show方法,show方法里面根本就沒做啥事,肯定是空白哦。
2016-05-04
2016-05-04
同樓上,要想調(diào)用內(nèi)部類的方法,就一定要?jiǎng)?chuàng)建內(nèi)部類的對(duì)象。
2016-05-04
MInner mi=new MInner();
mi.print();
2016-05-04
因?yàn)闆]有new內(nèi)部類的對(duì)象,只能執(zhí)行到外部方法。
2016-05-04
因?yàn)槟阍趕how方法中沒有new出MInner對(duì)象,然后調(diào)用print()方法。所以才會(huì)報(bào)錯(cuò)。
MInner mi=new MInner();
mi.print();
加上這個(gè)就好了