會先呼叫建構子來設定屬性
如果沒有撰寫建構子的話
Compiler 會執行預設建構子(Default Constructor)
預設建構子是不會做任何的事情的
建構子大概長得像下面這樣
public
Encrypt() {}
如果有參數的話會像這樣
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();
}
然而本篇提到的妙用
是指利用this()也可以當作
呼叫運算子
像是下面這樣
public
Encrypt(
int
a,
int
b) {
this
(a, b,
26
,
'a'
,
97
);
}
等同是呼叫
public
Encrypt(
int
a,
int
b,
int
n = 26,
char
c = 'a',
int
dif = 97) {
this
.a = a;
this
.b = b;
this
.n = n;
this
.c = c;
this
.diff = dif;
setArray();
}
蠻酷炫的!!
沒有留言:
張貼留言