我有問題,在我的腦海,爲什麼任何繼承靜態變量和方法是subclass.Isn`t它違反了關鍵字static的非靜態方法訪問? 代碼爲什麼繼承靜態變量可以通過子類的非靜態方法訪問?
package com.pack1;
public class MyClass {
static protected int testpro =100;
protected static void chkstat()
{
System.out.println("check stat");
}
}
package com.pack2;
import com.pack1.MyClass;
public class Test extends MyClass {
public void fromsup() {
System.out.println("" + this.testpro + "");
testpro = 200;
chkstat();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test tt = new Test();
tt.fromsup();
}
}
* 「Isn't它違反關鍵字的靜態?」 * - 關鍵字不能被 「侵犯」。最糟糕的是它可能與你的「靜態」應該如何表現的心理模型相矛盾。但那只是意味着你的心智模式是不正確的。 –