zoukankan      html  css  js  c++  java
  • 工厂模式


    //设计模式:工厂模式
    /*
    class Yunsuan
    {
    public $a;
    public $b;

    //public $f;

    function jia()
    {
    return $this->a + $this->b;
    }
    function jian()
    {
    return $this->a - $this->b;
    }
    function cheng()
    {
    return $this->a * $this->b;
    }
    function chu()
    {
    return $this->a / $this->b;
    }
    }
    $y=new Yunsuan();
    $y->a=10;
    $y->b=5;
    echo $y->jia();
    */
    //造父类,使用子类继承
    /*
    class Yunsuan
    {
    public $a;
    public $b;

    function Yunsuan()
    {

    }
    }
    //加法的子类
    class Jia extends Yunsuan
    {
    function Yunsuan()
    {
    return($this->a + $this->b);
    }
    }
    //减法的子类
    class Jian extends Yunsuan
    {
    function Yunsuan()
    {
    return($this->a-$this->b);
    }
    }

    $t=new Jia();
    $t->a=10;
    $t->b=4;
    echo $t->yunsuan();
    */

    //再优化,工厂类

    class Yunsuan
    {
    public $a;
    public $b;

    function Yunsuan()
    {

    }
    }
    //加法的子类
    class Jia extends Yunsuan
    {
    function Yunsuan()
    {
    return($this->a + $this->b);
    }
    }
    //减法的子类
    class Jian extends Yunsuan
    {
    function Yunsuan()
    {
    return($this->a-$this->b);
    }
    }
    //乘法的子类
    class Cheng extends Yunsuan
    {
    function Yunsuan()
    {
    return($this->a * $this->b);
    }
    }
    class Gongchang
    {

    static function DUixiang($f)
    {
    switch($f)
    {
    case"+";
    return new Jia();
    break;
    case"-";
    return new Jian();
    break;
    case"*";
    return new Cheng();
    break;
    }
    }
    }
    $r=Gongchang::DUixiang("+");
    $r->a=10;
    $r->b=5;
    echo $r->Yunsuan();

  • 相关阅读:
    Bootstrap框架
    前端基础之CSS
    前端知识之HTML内容
    python之路----递归函数
    python控制台输出带颜色文字的方法
    ELK搭建<三>:安装Kibana
    ELK搭建<二>:安装ES插件head
    ELK搭建<一>:搭建ES集群
    前端angular使用crypto-js进行加密
    Mac下全局安装yarn
  • 原文地址:https://www.cnblogs.com/pangchunlei/p/5575896.html
Copyright © 2011-2022 走看看