zoukankan      html  css  js  c++  java
  • PHP重构之函数上移

    参考《重构》

    <?php
    abstract class Customer
    {
        public function addBill($date, $amount)
        {
            echo "addBill()".'<br>';
        } 
        public function CreateBill($date)
        {
            echo "CreateBill()".'<br>';
            $this->chargeFor();
        }
        abstract protected function chargeFor();  // 将子类不同的函数部分抽象为抽象方法,让子类去实现
    }
    
    class RegularCustomer extends Customer
    {
        protected function chargeFor()
        {
            echo "go to north area, buy some food!".'<br>';
            echo "find a small shop eat noondles!".'<br>';
        }
    }
    
    class PreferredCustomer extends Customer
    {
        protected function chargeFor()
        {
            echo "buy drinks!!!".'<br>';
            echo "go to bank!!!".'<br>';
        }
    }
    
    $jack = new RegularCustomer();
    $mike = new PreferredCustomer();
    
    $jack->CreateBill("2013-08-12");
    $jack->addBill("2013-08-12", 35);
    echo "<br>".'<br>'.'<br>';
    $mike->CreateBill("2013-09-11");
    $mike->addBill("2013-09-11", 55);
    ?>
  • 相关阅读:
    ASP.NET教程4
    ASP.NET教程11
    TreeView Demo
    System.Net.Dns.GetHostByAddress(string) 已经过时
    会员注册实例
    ASP.NET教程2
    多表关联与表值函数
    ASP.NET教程6
    BusinessFrameWork
    ASP.NET教程8
  • 原文地址:https://www.cnblogs.com/Robotke1/p/3264728.html
Copyright © 2011-2022 走看看