protected
來談談protected訪問權限問題。看下面示例1:
Test.java
1
2
3
4
5
6
7
8
|
class MyObject {} public class Test { public static void main(String[] args) { MyObject obj = new MyObject(); obj.clone(); // Compile error. } } |
此時出現上文提到的錯誤:The method clone from the type Object is not visiuable.
我們已經清楚Object.clone()是protected方法。這說明,該方法可以被同包(java.lang)下和它(java.lang.Object)的子類訪問。這里是MyObject類(默認繼承java.lang.Object)。
同樣Test也是java.lang.Object的子類。但是,不能在一個子類中訪問另一個子類的protected方法,盡管這兩個子類繼承自同一個父類。
再看示例2:
Test2.java
1
2
3
4
5
6
7
8
9
10
11
12
|
class MyObject2 { protected Object clone() throws CloneNotSupportedException { return super .clone(); } } public class Test2 { public static void main(String[] args) throws CloneNotSupportedException { MyObject2 obj = new MyObject2(); obj.clone(); // Compile OK. } } |
這里,我們在MyObject2類中覆蓋(override)父類的clone()方法,在另一個類Test2中調用clone()方法,編譯通過。
編譯通過的原因顯而易見,當你在MyObject2類中覆蓋clone()方法時,MyObject2類和Test2類在同一個包下,所以此protected方法對Test2類可見。
分析到這里,我們在回憶一下Java中的淺復制與深復制文中,章節2.2中的聲明,②在派生類中覆蓋基類的clone()方法,并聲明為public。現在明白這句話的原因了吧(為了讓其它類能調用這個類的clone()方法,重載之后要把clone()方法的屬性設置為public)。
下面再來看示例3:
Test3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package 1 class MyObject3 { protected Object clone() throws CloneNotSupportedException { return super .clone(); } } package 2 public class Test3 extends MyObject3 { public static void main(String args[]) { MyObject3 obj = new MyObject3(); obj.clone(); // Compile error. Test3 tobj = new Test3(); tobj.clone(); // Complie OK. } } |
這里我用Test3類繼承MyObject3,注意這兩個類是不同包的,否則就是示例2的情形。在Test3類中調用Test3類的實例tobj的clone()方法,編譯通過。而同樣調用MyObject3類的實例obj的clone()方法,編譯錯誤!
意想不到的結果,protected方法不是可以被繼承類訪問嗎?
必須明確,類Test3確實是繼承了類MyObject3(包括它的clone方法),所以在類Test3中可以調用自己的clone方法。但類MyObject3的protected方法對其不同包子類Test3來說,是不可見的。
這里再給出《java in a nutshell》中的一段話:
protected access requires a little more elaboration. Suppose class A declares a protected field x and is extended by a class B, which is defined in a different package (this last point is important). Class B inherits the protected field x, and its code can access that field in the current instance of B or in any other instances of B that the code can refer to. This does not mean, however, that the code of class B can start reading the protected fields of arbitrary instances of A! If an object is an instance of A but is not an instance of B, its fields are obviously not inherited by B, and the code of class B cannot read them.
順便說兩句,國內的很多Java書籍在介紹訪問權限時,一般都這樣描述(形式各異,內容一致):
方法的訪問控制:
static
1.關鍵字static(先記住這些,再往下看)
1)靜態方法和靜態變量是屬于某一個類,而不屬于類的對象。
2)靜態方法和靜態變量的引用直接通過類名引用。
3)在靜態方法中不能調用非靜態的方法和引用非靜態的成員變量。反之,則可以。
4)靜態變量在某種程序上與其他語言的全局變量相類似,如果不是私有的就可以在類的外部進行訪問。
2.何時使用static
在我們創建一個類的實例時(對象),通常使用new方法,這樣這個類的數據空間才會被創建,其方法才能被調用。
但是,有時候我們希望一個類雖然可以被創建n個對象(顯然這n個對象的數據空間是不相同的),但這n個對象的某些數據是相同的,即不管這個類有多少的實例,這些數據對這些實例而言之有一份內存拷貝(見示例1)。這是靜態變量的情形。
另一種情形是,你希望某個方法不與包含它的類的任何對象關聯在一起。也就是說,即使沒有創建對象,也能夠調用這個方法。static 方法的一個重要用法就是在不創建任何對象的前提下,就可以調用它(見示例2)。這是靜態方法的情形。
還有一種特殊的用法出現在內部類中,通常一個普通類不允許聲明為靜態的,只有一個內部類才可以。這時這個聲明為靜態的內部類可以直接作為一個普通類來使用,而不需實例一個外部類(見示例3)。這是靜態類的情形。
示例1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class TStatic { static int i; public TStatic() { i = 4 ; } public TStatic( int j) { i = j; } public static void main(String args[]) { System.out.println(TStatic.i); TStatic t = new TStatic( 5 ); // 聲明對象引用,并實例化。此時i=5 System.out.println(t.i); TStatic tt = new TStatic(); // 聲明對象引用,并實例化。此時i=4 System.out.println(t.i); System.out.println(tt.i); System.out.println(t.i); } } |
結果:
1
2
3
4
5
|
0 5 4 4 4 |
static變量在類被載入時創建,只要類存在,static變量就存在。它們在定義時必須進行初始化。上例中沒有初始化i,所以會得到默認的初始值0。static的變量的初始化僅能一次,static變量只是接受了最后一次的初始化。
實際這還是多個實例共享一個靜態的變量的問題。
示例2
未聲明為static
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class ClassA { int b; public void ex1() {} class ClassB { void ex2() { int i; ClassA a = new ClassA(); i = a.b; // 這里通過對象引用訪問成員變量b a.ex1(); // 這里通過對象引用訪問成員函數ex1 } } } |
聲明為static
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class ClassA { static int b; static void ex1() {} } class ClassB { void ex2() { int i; i = ClassA.b; // 這里通過類名訪問成員變量b ClassA.ex1(); // 這里通過類名訪問成員函數ex1 } } |
在使用靜態方法時要注意,在靜態方法中不能調用非靜態的方法和引用非靜態的成員變量(在static方法中也不能以任何方式引用this或super)。理由很簡單,對于靜態的東西,JVM在加載類時,就在內存中開辟了這些靜態的空間(所以可以直接通過類名引用),而此時非靜態的方法和成員變量所在的類還沒有實例化。
所以如果要使用非靜態的方法和成員變量,可以直接在靜態方法中實例化該方法或成員變量所在的類。public static void main就是這么干的。
示例3
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class StaticCls { public static void main(String[] args) { OuterCls.InnerCls oi = new OuterCls.InnerCls(); // 這之前不需要new一個OuterCls } } class OuterCls { public static class InnerCls { InnerCls() { System.out.println( "InnerCls" ); } } } |
結果:
1
|
InnerCls |
3.靜態初始化
static定義的變量會優先于任何其它非static變量,不論其出現的順序如何。靜態代碼塊(在“static{”后面跟著一段代碼),是用來進行顯式的靜態變量初始化,這段代碼只會初始化一次,且在類被第一次裝載時。看下面示例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
class Value { static int c = 0 ; Value() { c = 15 ; } Value( int i) { c = i; } static void inc() { c++; } } class Count { public static void prt(String s) { System.out.println(s); } Value v = new Value( 10 ); static Value v1, v2; static { prt( "in the static block of calss Count v1.c=" + v1.c + " v2.c=" + v2.c); v1 = new Value( 27 ); prt( "in the static block of calss Count v1.c=" + v1.c + " v2.c=" + v2.c); v2 = new Value(); prt( "in the static block of calss Count v1.c=" + v1.c + " v2.c=" + v2.c); } } public class TStaticBlock { public static void main(String[] args) { Count ct = new Count(); Count.prt( "in the main:" ); Count.prt( "ct.c=" + ct.v.c); Count.prt( "v1.c=" + Count.v1.c + " v2.c=" + Count.v2.c); Count.v1.inc(); Count.prt( "v1.c=" + Count.v1.c + " v2.c=" + Count.v2.c); Count.prt( "ct.c=" + ct.v.c); } } |
結果:
1
2
3
4
5
6
7
8
|
in the static block of calss Count v1.c=0 v2.c=0 in the static block of calss Count v1.c=27 v2.c=27 in the static block of calss Count v1.c=15 v2.c=15 in the main: ct.c=10 v1.c=10 v2.c=10 v1.c=11 v2.c=11 ct.c=11 |
不管是v,v1還是v2,它們操作的成員變量都是同一個靜態變量c。
在類Count中先初始化v1,v2(static Value v1, v2;),再初始化靜態代碼塊(static{}),最后初始化v。