請問這是哪里錯了
package abc;
public class HelloWorld {
private static int score=87;
public class appcf{
int score=68;
public void abc() {
System.out.println("外部類score:"+HelloWorld.score);
System.out.println("內部類score:"+score);
}
}
public static void main(String[]args) {
appcf o=new appcf();
o.abc();
}
}
2018-01-02
貌似你在定義appcf類的時候沒有設置靜態static,在System.out.println();中輸出的是直接外部類.靜態成員變量,造成你的報錯,如果是你這樣進行寫跟樓上的this.成員變量才對,他這是引用的不是靜態的成員變量的用法;也是剛開始學學,相互比較得出的,清指教;
2017-11-09
public class HelloWorld {
private static int score=87;
public class appcf{
int score=68;
public void abc() {
System.out.println("外部類score:"+HelloWorld.this.score);
System.out.println("內部類score:"+score);
}
}
public static void main(String[]args) {
HelloWorld hello = new HelloWorld();
appcf o=hello.new appcf();
o.abc();
}
}
2017-10-10
應該是System.out.println("外部類score:"+HelloWorld.score);這句有問題
我覺得應該改成System.out.println("外部類score:"+HelloWorld.this.score);
我也是剛好學到這兒