亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

關于靜態內部類直接訪問外部類的非靜態成員 new 外部類().成員 能不能賦值

//外部類

public class HelloWorld{

? private int num = 1;

? // 外部類中的靜態變量score

? private static int score = 84;

??

? // 創建靜態內部類

public? ? ?static? ? ?class SInner {

? ? ? // 內部類中的變量score

? ? ? int score = 91;

? ? ??

public void show() {

System.out.println("訪問外部類中的score:" +? ? ?HelloWorld.score? ? ? );

System.out.println("訪問內部類中的score:" + score);

HelloWorld hello = new HelloWorld();

System.out.println(hello.num);//這句對的

public int a = hello.num;//這句錯了

}

}


// 測試靜態內部類

public static void main(String[] args) {

// 直接創建內部類的對象

? ? ? SInner si = new SInner();

? ? ??

? ? ? // 調用show方法

si.show();

System.out.println(si.a);//這句錯了

}

}

上面的這三句

public int a = hello.num;

System.out.println(si.a);

這兩句是錯的

System.out.println(hello.num);

這句是對的


正在回答

2 回答

不好意思,看著這個代碼上的get方法,覺得作用會不會不大?

0 回復 有任何疑惑可以回復我~

已解決

我把東西寫在函數里了。。。

下面是對的

package com.test.second;


//外部類

public class test2 {

? private int num = 1;

? // 外部類中的靜態變量score

? private static int score = 84;

??

? // 創建靜態內部類

public? ? ?static? ? ?class SInner {

? ? ? // 內部類中的變量score

? ? ? int score = 91;

? ? ??

public void show() {

System.out.println("訪問外部類中的score:" +? ? ?test2.score? ? ? );

System.out.println("訪問內部類中的score:" + score);

}

public int getNum() {

return a;

}

// test2 hello = new test2();

// private int a = hello.num;

int a = new test2().num;

}


// 測試靜態內部類

public static void main(String[] args) {

// 直接創建內部類的對象

? ? ? SInner si = new SInner();

? ? ??

? ? ? // 調用show方法

si.show();

System.out.println(si.a);

}

}


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

關于靜態內部類直接訪問外部類的非靜態成員 new 外部類().成員 能不能賦值

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號