請(qǐng)問(wèn)這樣的寫法不可以嗎?
//外部類
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加分后的成績(jī):" + newScore);
?}
???
?// 測(cè)試方法內(nèi)部類
?public static void main(String[] args) {
???????
??// 創(chuàng)建外部類的對(duì)象
??????? HelloWorld mo = new HelloWorld();
???????
??????? // 調(diào)用外部類的方法
??mo.show();
?}
}
2016-11-13
這個(gè)newScore是你創(chuàng)建的內(nèi)部類對(duì)象,不是一個(gè)變量,這塊應(yīng)該是newScore.getjScore();
2016-11-15
newScore只是你創(chuàng)建的對(duì)象,你沒賦值,沒定義變量類型沒用,如果想要直接調(diào)用內(nèi)部類的方法顯示如下就可以:
System.out.println("姓名:" + name + "\n加分后的成績(jī):" + minner.getScore());
2016-11-13
如果想要獲取地址,則需要調(diào)用toString()的方法
2016-11-13
方法內(nèi)部類只能在方法中調(diào)用