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

    <?php 
              //php策略模式
             interface Math{
                 public function calc($op1,$op2);
             }
             class MathAdd implements Math{
               public function calc($op1,$op2){
                 return $op1+$op2;
                }
             }
              class MathSub implements Math{
               public function calc($op1,$op2){
                 return $op1-$op2;
                }
             }
              class MathMul implements Math{
               public function calc($op1,$op2){
                 return $op1*$op2;
                }
             }
              class MathDiv implements Math{
               public function calc($op1,$op2){
                 return $op1/$op2;
                }
             }
    //封装一个虚拟计算机
          class CMath{
               protected $calc=null;
               public function __construct($type){
                    $calc='Math'.$type;
                    $this->calc=new $calc();
               }
               public function calc($op1,$op2){
                return   $this->calc->calc($op1,$op2);
                }
          }
        $type=$_POST['op'];
        $cmath=new CMath($type);
        echo  $cmath->calc($_POST['op1'],$_POST['op2']);
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>策略模式</title>
    </head>
    <body>
            <form action='2.php' method="post">
                 <input type="text" name="op1">
                 <select name="op">
                            <option value="add">+</option>
                            <option value="sub">-</option>
                            <option value="mul">*</option>
                            <option value="div">/</option>
                   </select>
                <input type="text" name="op2">
                <p><input type="submit" name="" value="计算"></p>
            </form>
    </body>
    </html>

    策略模式实现简单的计算器功能

  • 相关阅读:
    标准化R包开发流程
    创建Rdemo项目
    rJava在ubuntu上的安装
    Linux初始root密码设置
    检查网卡错误
    统计学习方法-李航 第一章
    ubuntu16.04细节设置
    linux指令学习
    Python在ubuntu16.04上环境搭建
    kuberneets 1.17 设置 kube-reserved, system-reserved
  • 原文地址:https://www.cnblogs.com/kangshuai/p/5788446.html
Copyright © 2011-2022 走看看