日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

php設計模式介紹之值對象模式(4)_PHP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:談PHP程序開發(fā)中的中文編碼問題
PHP程序設計中中文編碼問題曾經(jīng)困擾很多人,導致這個問題的原因其實很簡單,每個國家(或區(qū)域)都規(guī)定了計算機信息交換用的字符編碼集,如美國的擴展 ASCII 碼, 中國的 GB2312-80,日本的 JIS 等。作為該國家/區(qū)域內信息處理的基礎,字符編碼集起著統(tǒng)一編碼的

另一個重要的概念是對象Monopoly中的租金支付。讓我們首先寫一個測試實例(測試引導開發(fā))。下面的代碼希望用來實現(xiàn)既定的目標。

function TestRent() {

$game = new Monopoly;

$player1 = new Player(‘Madeline’);

$player2 = new Player(‘Caleb’);

$this->assertEqual(1500, $player1->getBalance());

$this->assertEqual(1500, $player2->getBalance());

$game->payRent($player1, $player2, new Dollar(26));

$this->assertEqual(1474, $player1->getBalance());

$this->assertEqual(1526, $player2->getBalance());

}

根據(jù)這個測試代碼,我們需要在Monopoly對象中增加payRent()的方法函數(shù)來實現(xiàn)一個Player對象去支付租金給另一個Player對象.

Class Monopoly {

// ...

/**

* pay rent from one player to another

* @param Player $from the player paying rent

* @param Player $to the player collecting rent

* @param Dollar $rent the amount of the rent

* @return void

*/

public function payRent($from, $to, $rent) {

$to->collect($from->pay($rent));

}

}

payRent()方法函數(shù)實現(xiàn)了兩個player對象之間($from和$to)的租金支付。方法函數(shù)Player::collect()已經(jīng)被定義了,但是Player::pay()必須被添加進去,以便實例$from通過pay()方法支付一定的Dollar數(shù)額$to對象中。首先我們定義Player::pay()為:

class Player {

// ...

public function pay($amount) {

$this->savings = $this->savings->add(-1 * $amount);

}

}

但是,我們發(fā)現(xiàn)在PHP中你不能用一個數(shù)字乘以一個對象(不像其他語言,PHP不允許重載操作符,以便構造函數(shù)進行運算)。所以,我們通過添加一個debit()方法函數(shù)實現(xiàn)Dollar對象的減的操作。

class Dollar {

protected $amount;

public function __construct($amount=0) {

$this->amount = (float)$amount;

}

public function getAmount() {

return $this->amount;

}

public function add($dollar) {

return new Dollar($this->amount $dollar->getAmount());

}

public function debit($dollar) {

return new Dollar($this->amount - $dollar->getAmount());

}

}

引入Dollar::debit()后,Player::pay()函數(shù)的操作依然是很簡單的。

class Player {

// ...

/**

* make a payment

* @param Dollar $amount the amount to pay

* @return Dollar the amount payed

*/

public function pay($amount) {

$this->savings = $this->savings->debit($amount);

return $amount;

}

}

Player::pay()方法返回支付金額的$amount對象,所以Monopoly::payRent()中的語句$to->collect($from->pay($rent))的用法是沒有問題的。這樣做的話,如果將來你添加新的“商業(yè)邏輯”用來限制一個player不能支付比他現(xiàn)有的余額還多得金額。(在這種情況下,將返回與player的賬戶余額相同的數(shù)值。同時,也可以調用一個“破產(chǎn)異常處理”來計算不足的金額,并進行相關處理。對象$to仍然從對象$from中取得$from能夠給予的金額。)

注:術語------商業(yè)邏輯

在一個游戲平臺的例子上提及的“商業(yè)邏輯”似乎無法理解。這里的商業(yè)的意思并不是指正常公司的商業(yè)運作,而是指因為特殊應用領域需要的概念。請把它認知為 “一個直接的任務或目標”,而不是“這里面存在的商業(yè)操作”。

所以,既然目前我們討論的是一個Monopoly,那么這里的 “商業(yè)邏輯”蘊含的意思就是針對一個游戲的規(guī)則而說的。

分享:淺談正確理解PHP程序錯誤信息的表示含義
簡述:我們編寫程序時,無論怎樣小心謹慎,犯錯總是在所難免的。這些錯誤通常會迷惑PHP編譯器。如果開發(fā)人員無法了解編譯器報錯信息的含義,那么這些錯誤信息不僅毫無用處,還會常常讓人感到沮喪。 我們編寫程序時,無論怎樣小心謹慎,犯錯總是在所難免的。

來源:模板無憂//所屬分類:PHP教程/更新時間:2009-06-18
相關PHP教程