在創(chuàng)建方法內(nèi)部類對(duì)象時(shí),這么做不可以么,代碼可以運(yùn)行,但是結(jié)果不對(duì)
//外部類
public class HelloWorld {
??
? private String name = "愛慕課";
??
? // 外部類中的show方法
? public void show() {?
// 定義方法內(nèi)部類
class MInner {
int score = 83;
public int getScore() {
return score + 10;
}
}
// 創(chuàng)建方法內(nèi)部類的對(duì)象
? ? ? MInner newScore=new MInner();
? ? ??
? ? ? // 調(diào)用內(nèi)部類的方法
newScore.getScore();
? ? ??
System.out.println("姓名:" + name + "\n加分后的成績:" + newScore);
}
??
// 測(cè)試方法內(nèi)部類
public static void main(String[] args) {
? ? ??
// 創(chuàng)建外部類的對(duì)象
? ? ??
? ? ? HelloWorld mo=new HelloWorld();
? ? ? // 調(diào)用外部類的方法
mo.show();
}
}
2017-04-09
getScore方法有返回值,返回值要保存在一個(gè)變量中。