欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

设计模式之模板模式

程序员文章站 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