作者samtree (嗯....?)
看板java
標題Re: [問題] constructor invoke another constructor
時間Sat Sep 22 00:31:08 2012
※ 引述《NPcomplete (Non-deterministic)》之銘言:
: Hello everyone,
: "The benefit of having one constructor invoke another overloaded constructor
: is to avoid code duplication."
: 讀書時看到這句並不了解為什麼
: 想請問有沒有人可以解釋
: 或是直接給我一個例子
: 感謝~~
這樣子的話有感覺嗎?
Case 1:
public 綠茶() {
綠茶(正常, 正常);
}
public 綠茶(甜度, 冰塊) {
this.甜度 = 甜度;
this.冰塊 = 冰塊;
}
Case 2:
public 綠茶() {
this.甜度 = 正常;
this.冰塊 = 正常;
}
public 綠茶(甜度, 冰塊) {
this.甜度 = 甜度;
this.冰塊 = 冰塊;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.250.54.136
推 kewang:有感覺 XD 09/22 13:06
推 superlubu:超有感覺的啊 XDDD 09/22 17:33
→ NPcomplete:謝謝 09/22 20:08
推 SansWord:很好! 09/30 00:52