為什么這樣不行?用對象外部類調用外部類方法,結果無法執行到內部類方法?
ublic class HelloWorld?
{
? ? // 外部類中的show方法
? ? public void show()?
? ? {?
// 定義方法內部類
? ?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
因為你在show()方法中沒有調用內部類的print()方法;
你可以在show()中生成一個內部類對象,用這個對象調用print()方法;
如:
MInner mi = new MInner();
? ? ? ? mi.print();
2016-05-04
因為你這樣子調用show方法,show方法里面根本就沒做啥事,肯定是空白哦。
2016-05-04
2016-05-04
同樓上,要想調用內部類的方法,就一定要創建內部類的對象。
2016-05-04
MInner mi=new MInner();
mi.print();
2016-05-04
因為沒有new內部類的對象,只能執行到外部方法。
2016-05-04
因為你在show方法中沒有new出MInner對象,然后調用print()方法。所以才會報錯。
MInner mi=new MInner();
mi.print();
加上這個就好了