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