设计模式之模板模式
程序员文章站
2022-07-13 23:54:46
...
<?php
/**
* 模板模式
* Created by PhpStorm.
* User: chen
* Date: 2018/11/6
* Time: 9:53
*/
abstract class Account
{
protected $principal;//本金
protected $interest;//利息
function getInterest() {
return $this->principal * $this->getInterestRate();
}
public function setPrincipal($principal)
{
$this->principal = $principal;
}
abstract function getInterestRate();
}
class ABC extends Account
{
public function getInterestRate()
{
return 0.5;
}
}
class ICBC extends Account
{
public function getInterestRate()
{
return 0.6;
}
}
$xiaoMing = new ABC();
$xiaoMing->setPrincipal(500);
echo $xiaoMing->getInterest();//250
echo '<br>';
$xiaoLang = new ICBC();
$xiaoLang->setPrincipal(500);
echo $xiaoLang->getInterest();//300
上一篇: PHP设计模式——状态模式
下一篇: 23种设计模式之模板