靜態修飾符與可見性無關,所以沒有什麼會改變。不能訪問受保護的成員(即字段,方法)來自不同的包,無論它是否是靜態的。
但在你的情況下,MyClass2擴展了MyClass1。您可以從超類訪問受保護的成員。仍然,靜態與此無關。
編輯:
那麼,這是因爲涉及兩個不同的東西非常有趣的案例: 1)訪問從子類保護成員在不同的包, 2)語法技巧允許您訪問靜態(類)成員喜歡實例成員。
我會延長你的例子來說明什麼是真正的你在這個代碼做:
package p1;
public class MyClass1 {
protected String protectedString = "example";
protected static String protectedStaticString = "example";
}
package p2;
import p1.MyClass1;
public class MyClass2 extends MyClass1 {
public void testProtected() {
MyClass1 otherMC1 = new MyClass1();
MyClass2 otherMC2 = new MyClass2();
String testProtected;
testProtected = this.protectedString; // OK, protectedString is inherited, so it's instance member of this class
testProtected = super.protectedString; // OK, it's also instance member of superclass
testProtected = otherMC1.protectedString; // ERROR. You can't access protected members of other instance of superclass
testProtected = otherMC2.protectedString; // OK. As it's inherited member of MyClass2, you can access it if it belongs to other instance of your class
}
public void testProtectedStatic() {
MyClass1 otherMC1 = new MyClass1();
MyClass2 otherMC2 = new MyClass2();
String testProtectedStatic;
testProtectedStatic = this.protectedStaticString; // OK - syntax trick
testProtectedStatic = super.protectedStaticString; // OK - syntax trick
testProtectedStatic = otherMC1.protectedStaticString; // OK - syntax trick
testProtectedStatic = otherMC2.protectedStaticString; // OK - syntax trick
testProtectedStatic = MyClass1.protectedStaticString; // OK - you can access protected static members from superclass
testProtectedStatic = MyClass2.protectedStaticString; // OK - this also static member of your class
}
}
現在,什麼是「語法絕招」。編譯器允許您訪問靜態成員,如他們是實例成員:
MyClass mc = new MyClass();
Object o = mc.STATIC_FIELD;
但是,將實際編譯成字節碼:
MyClass mc = new MyClass();
Object o = MyClass.STATIC_FIELD;
訪問靜態成員以這種方式被認爲是不好的做法,因爲這是誤導。您應該始終以靜態方式訪問靜態成員,以避免在閱讀代碼時出現混淆。
這是什麼在你的情況。有靜態字段以非靜態方式訪問,所以它看起來像實例字段。當您刪除靜態修飾符時,它成爲實例成員。訪問該字段的方式發生了變化,但您沒有注意到,因爲您的語法爲object.staticField。
的確如此,我剛剛注意到了這一點,並且我編輯了答案。 – 2013-04-05 19:53:29
您可以通過繼承從不同的包進行訪問。 – elias 2013-04-05 19:54:01
感謝您的回答。但是,如果String str只受保護,爲什麼它不起作用呢?我仍然從子類訪問它。 – user2250448 2013-04-05 22:16:35