zoukankan      html  css  js  c++  java
  • php策略模式的研究

    <?php

    abstract class Moshi{

        private $num;
        public $price;
        const Ted=1;
        const Sed=2;

        public function __construct($num,$price)
        {
            $this->num = $num;
            $this->price = $price;
        }

        public function cost(){
            if($this->price == self::Ted) return 30;
            elseif($this->price == self::Sed) return 5*$this->num;
            else { $this->price == self::Ted; return 35;}
        }

        public function ccc(){
            if($this->price == self::Ted) return 'a';
            elseif($this->price == self::Sed) return 'b';
            else$this->price == self::Ted; return 35;}
        }

    }

    class Aa extends Moshi{}
    class Bb extends Moshi{}
    var_dump(Moshi::Ted);//类中常量的调用
    $a=new Aa(5,Moshi::Ted);
    print "{$a->cost()}--{$a->ccc()} ";
    $b=new Aa(3,Moshi::Sed);
    print "{$b->cost()}--{$b->ccc()} ";

    $c=new Aa(5,4);
    print "{$c->cost()}--{$c->ccc()}--{$c->price} ";
    <?php

    abstract class Moshi2{

        private $num;
        private $price;

        public function __construct($num,Sget $sget)
        {
            $this->num = $num;
            $this->price = $sget;
        }

        public function cost(){
            return $this->price->cost($this);

        }

        public function ccc(){
            return $this->price->charge();
        }

        public function gets(){
            return $this->num;
        }
    }

    class Aa extends Moshi2{}
    class Bb extends Moshi2{}

    abstract class Sget{
        abstract function cost(Moshi2 $moshi2);
        abstract function charge();
    }

    class Ee extends Sget{
        function cost(Moshi2 $moshi2){
            return $moshi2->gets() * 5;
        }
        function charge(){
            return 'a';
        }
    }

    class Ff extends Sget{
        function cost(Moshi2 $moshi2){
            return 30;
        }
        function charge(){
            return 'b';
        }
    }


    $bb    = new Aa(4,new Ee());
    print "{$bb->cost()} ";

    $les[] = new Aa(4,new Ee());
    $les[] = new Bb(4,new Ff());
    var_dump($les);
    foreach($les as $val){
        print "{$val->cost()}--";
        print "{$val->ccc()} ";

    }
  • 相关阅读:
    nodejs日志管理log4js
    js数组去重的几种方法
    Echarts同一页面多个图表自适应浏览器窗口大小——window.onresize
    Echarts自适应浏览器大小
    [原创]Nodejs 远程执行linux shell
    ssh无需密码登录linux服务器
    Nodejs mysql pool使用实例
    Activity基础知识
    最大熵模型介绍
    node-sass element-plus 安装失败
  • 原文地址:https://www.cnblogs.com/jami918/p/3597144.html
Copyright © 2011-2022 走看看