相思资源网 Design By www.200059.com

本文实例讲述了PHP基于工厂模式实现的计算器。分享给大家供大家参考。具体如下:

abstract class Calculator
{
 private $number1;
 private $number2;
 public $result;
 /**
  * @return the $number2
  */
 public function getNumber2() {
  return $this->number2;
 }
 /**
  * @param field_type $number2
  */
 public function setNumber2($number2) {
  $this->number2 = $number2;
 }
 /**
  * @return the $number1
  */
 public function getNumber1() {
  return $this->number1;
 }
 /**
  * @param field_type $number1
  */
 public function setNumber1($number1) {
  $this->number1 = $number1;
 }
 abstract function get_result();
 }
class Add extends Calculator
{
  public function get_result($number1,$number2)
  {
   return $number1+$number2;
  }
}
class Sub extends Calculator
{
  public function get_result($number1,$number2)
  {
   return $number1-$number2;
  }
}
class Mul extends Calculator
{
  public function get_result($number1,$number2)
  {
   return $number1*$number2;
  }
}
class Div extends Calculator
{
  public function get_result($number1,$number2)
  {
   return $number1/$number2;
  }
}
class Factory
{
  public function Building($notes)
  {
   if($notes=="+")
   {
    $add=new Add();
    return $add;
   }
   elseif ($notes=="-")
   {
    $sub=new Sub();
    return $sub;
   }
   elseif($notes=="*")
   {
    $mul=new Mul();
    return $mul;
   }
   else
   {
    $div=new Div();
    return $div;
   }
  }
}

希望本文所述对大家的php程序设计有所帮助。

标签:
PHP,工厂模式,计算器

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“PHP基于工厂模式实现的计算器实例”

暂无PHP基于工厂模式实现的计算器实例的评论...