策略模式
发布时间:2022-10-23 03:11:33 276
相关标签: # 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($op1, $op2);
文章来源: https://blog.51cto.com/u_12903656/5199841
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报