facebook廣告





123

2017年1月28日 星期六

Java 入門指南 - 建構子

Java 入門指南 - 建構子

Java 中建立物件 (object) 需要建構子 (constructor) ,如果類別 (class) 沒有定義建構子,編譯器 (compiler) 就會提供一個預設的建構子 (default constructor)




我們現在要來詳細討論建構子的作用與功能,建構子的主要目的在於建立物件的過程中,也就是使用 new 關鍵字 (keyword) 的時候,程式 (program) 建立物件所執行的工作,這些工作通常就是初始化物件的屬性 (field) 。


如果類別沒有定義建構子,編譯器會提供預設預購子,預設建構子為沒有參數 (parameter) 的版本
public Encrypt() {}


須注意,如果我們有在類別中定義建構子,編譯器便不會提供沒有參數版本的預設建構子。建構子可以依需求而有不同的參數版本,例如 Encrypt 中我們需要這些屬性
char[] cArray;
int a;
int b;
int n;
char c;
int diff;


其中 a 、 b 、 n 、 c 、 diff ,我們需要提供這五個屬性值,然後呼叫 setArray() 計算出 cArray 的值
public Encrypt(int a, int b, int n, char c, int dif) {
    this.a = a;
    this.b = b;
    this.n = n;
    this.c = c;
    this.diff = dif;
    setArray();
}


當然,有些屬性值也可以用預設的,這就是形成不同參數版本的建構子
public Encrypt(int a, int b) {
    this(a, b, 26, 'a', 97);
}


這是利用 this 呼叫最多參數版本的建構子, a 、 b 為此建構子所需要提供的參數,其餘三個 n 、 c 、 diff ,我們直接以字面常數 (literal) 代入預設值, 也就是 n 為 26 , c 為 'a' , diff 為 97 。


可是 Encrypt 不是要 a 、 b 都為 0 到 9 隨機整數的隨機值嗎?我們仍得在 EncryptDemo 中進行設定嗎?不,不需要,我們可以設計沒有參數版本的建構子
public Encrypt() {
    int ta = 0;
    int tb = 0;
         
    while (ta % 2 == 0) {
        ta = (int) (Math.random() * 10);
        tb = (int) (Math.random() * 10);
    }
         
    this.a = ta;
    this.b = tb;
    this.n = 26;
    this.c = 'a';
    this.diff = 97;
    setArray();
}


這裡,屬性設定的部份也可以這樣寫
a = ta;
b = tb;
n = 26;
c = 'a';
diff = 97;
setArray();


我們並沒有用到與屬性名稱相同的區域變數 (local variable) ,所以可以直接以屬性名稱設定數性質。我們仍使用 this 的原因很簡單,就是既然前兩個建構子都有用到 this ,我們這裡維持一致性,所以也用 this 來設定屬性。


完整的 Encrypt 如下
?
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class Encrypt {
    // 屬性
    char[] cArray = new char[26];
    int a;
    int b;
    int n;   
    char c; 
    int diff;
     
    // 建構子
    public Encrypt(int a, int b, int n, char c, int dif) {
        this.a = a;
        this.b = b;
        this.n = n;
        this.c = c;
        this.diff = dif;
        setArray();
    }
 
    public Encrypt(int a, int b) {
        this(a, b, 26, 'a', 97);
    }
 
    public Encrypt() {
        int ta = 0;
        int tb = 0;
         
        while (ta % 2 == 0) {
            ta = (int) (Math.random() * 10);
            tb = (int) (Math.random() * 10);
        }
         
        this.a = ta;
        this.b = tb;
        this.n = 26;
        this.c = 'a';
        this.diff = 97;
        setArray();
    }   
     
    // setter 方法
    public void setArray() {
        int i, y, x, m;
        char r;
         
        x = c;
        for (i = 0; i < n; i++) {
            y = x * a + b;
            m = y % n;
            r = (char) (m + diff);
            cArray[i] = r;
            x++;
        }       
    }
}
 
/* 《程式語言教學誌》的範例程式
    檔名:Encrypt.java
    功能:示範 Java 程式
    作者:張凱慶
    時間:西元 2011 年 4 月 */


然後修改 EncryptDemo 進行測試
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 測試 Encrypt 的類別
public class EncryptDemo {
    public static void main(String[] args) {
        System.out.println();
        for (int i = 0; i < 16; i++) {
            Encrypt t = new Encrypt();
            System.out.println(t.cArray);
        }
        System.out.println();
    }
}
 
/* 《程式語言教學誌》的範例程式
    檔名:EncryptDemo.java
    功能:示範 Java 程式
    作者:張凱慶
    時間:西元 2011 年 4 月 */


記得 Encrypt 與 EncryptDemo 都修改過,因此執行前都要先進行編譯,如下



這裡,我們在 EncryptDemo 中直接用句點運算子存取屬性 cArray ,其實不一個好習慣,因為如此物件的資料就曝光在世人面前,任何人都可以透過句點運算子存取,甚至更改屬性值,造成難以彌補的後果,從而失去資訊隱藏 (information hiding) 的意義。


因此,我們應該將屬性設成 private ,然後以 getter 與 setter 進行操作哩!


中英文術語對照
物件object
建構子constructor
類別class
編譯器compiler
建構子default constructor
關鍵字keyword
程式program
屬性field
參數parameter
字面常數literal
區域變數local variable
資訊隱藏information hiding

沒有留言:

張貼留言