爱程序网

使用面向对象的图形计算器

来源: 阅读:

这个例子可能并不实用,但基本概括了面向对象的三个特征:继承性,封装性,多态性。本例的主要功能有:

  1. 让用户可以选择不同类型的图形;
  2. 对所选的图形输入其相关属性;
  3. 根据输入的属性计算该图形的周长和面积。

效果如下:

思路:

    1. A部分直接在index.php中写,点击对应是图形的时候发送一个$_GET["shape"]给自身页面,使用了自动加载类。
    2. B部分由form.class.php输出,其中使用了变量函数,用$_GET["shape"]的值调用不同的函数,确定不同图形的表单中input部分。
    3. C部分由result.class.php输出。声明一个抽象类,在rect,triangle,circle中实现抽象类中计算面积和周长的计算方法,体现继承性,封装性和多态性,使用new $_GET["shape"]()实例化对应图形的对象,再调用该对象中的方法,返回周长和面积。

需要改进的地方:

  1. 本例子只是为了作为演示类的几个特性,并没有对用户的输入进行过滤,可能造成注入攻击,不适用于实际生产应用。实际应用时应该对用户的输入进行过滤,防止恶意攻击。
  2. 没有用DIV+CSS对页面布局进行优化,界面不是很友好。可以优化布局,改善用户体验。

index.php代码如下:

 1 <html> 2 <head> 3         <meta http-equiv="charset" content="utf-8"> 4 </head> 5 <body> 6         <div id="center"> 7         <h1>图形周长面积计算器</h1> 8         <!--点击链接的时候使用GET传递图形的形状属性给index.php,也就是页面本身--> 9         <a href="index.php?shape=rect">矩形</a>10         <a href="index.php?shape=triangle">三角形</a>11         <a href="index.php?shape=circle">圆形</a>12         </div>13         <div id="inputForm">14 <?php15         /*自动加载类*/16         function __autoload($className){17                 include (strtolower($className).'.class.php');18         }19 20         /*21         1.先new一个Form对象,发现没有form类的定义,把类名Form传递到自动加载类的函数参数进行类的自动加载。22         2.echo一个对象的引用,会调用该对象的__toString方法返回一个字符串,echo输出的就是对象返回的字符串,23           这里输出一个表单等待用户的输入。24         */25         echo new Form("index.php");26 27         /*如果用户点击了提交按钮,自动加载result类,输出结果*/28         if(isset($_POST["sub"])){29                 echo new result();30         }31 ?>32         </div>33 </body>34 </html>

form.class.php代码如下:

 1 <?php 2         /* 3         project:面向对象版图形计算器 4         file:form.class.php 5         description:对不同的图形输出不同的表单 6         */ 7         class form{ 8                 private $formAction=NULL;       //保存响应表单的文件 9                 private $shape=NULL;            //保存图形的形状10 11                 /*12                 @param string $action 对象初始化传入的参数,代表响应的页面的是哪一个文件13                 */14                 function __construct($action = ""){15                         $this->formAction = $action;    //把传入的参数保存到$formAction中;16                         $this->shape = isset($_GET["shape"]) ? $_GET["shape"]:"rect";   //从表单传递的变量中获取图形类别,如没有传递,默认为矩形17                 }18                 function __toString(){19                         $form = '<form action="'.$this->formAction.'?shape='.$this->shape.'" method="post">';20                         //下面两行使用变量函数调用对应图形的私有函数,返回input部分表单的字符串21                         $shape = 'get'.ucfirst($this->shape);22                         $form .= $this->$shape();23 24                         $form .= '</br><input type="submit" name="sub" value="计算"/></br>';25                         $form .= '</form>';26 27                         return $form;28                 }29                 //私有方法,返回矩形表单input部分的字符串;30                 private function getRect(){31                         //在表单提交后输入的内容继续显示,防止其消失32                         $formheight=isset($_POST['height']) ? $_POST['height'] : NULL;33                         $formwidth=isset($_POST['width']) ? $_POST['width'] : NULL;34                         $input = '<p>请输入矩形的长和宽</p>';35                         $input .= '矩形的高度:<input type="text" name="height" value="'.$formheight.'"/><br></br>';36                         $input .= '矩形的宽度:<input type="text" name="width" value="'.$formwidth.'"/></br>';37                         return $input;38                 }39                 //返回三角形输入表单input部分的字符串40                 private function getTriangle(){41                         //在表单提交后继续显示出来,防止其消失42                         $formside1=isset($_POST['side1']) ? $_POST['side1'] : NULL;43                         $formside2=isset($_POST['side2']) ? $_POST['side2'] : NULL;44                         $formside3=isset($_POST['side3']) ? $_POST['side3'] : NULL;45                         $input = '<p>请输入三角形的三边</p>';46                         $input .= '边长1:<input type="text" name="side1" value="'.$formside1.'" /></br></br>';47                         $input .= '边长2:<input type="text" name="side2" value="'.$formside2.'"/></br></br>';48                         $input .= '边长3:<input type="text" name="side3" value="'.$formside3.'"/></br>';49                         return $input;50                 }51                 //返回圆形表单input部分的字符串52                 private function getCircle(){53                         $formradius=isset($_POST['radius']) ? $_POST['radius'] : NULL;  //在输入表单提交后内容继续显示出来,防止其消失54                         $input = '<p>请输入半径</p>';55                         $input .= '半径:<input type="text" name="radius" value="'.$formradius.'"/></br>';56                         return $input;57                 }58         }59                                            

 result.class.php代码如下:

 1 <?php 2 class result{ 3         private $shape = NULL; 4  5         //使用GET传递的变量,实例化一个相应的对象,返回一个对象的引用; 6         function __construct(){ 7                 $this->shape = new $_GET["shape"](); 8         } 9         //调用对象的属性和方法,返回周长和面积10         function __toString(){11                 $result = $this->shape->shapeName.'的周长为'.$this->shape->perimeter().'</br>';12                 $result .= $this->shape->shapeName.'的面积为'.$this->shape->area().'</br>';13                 return $result;14         }15 }                                                                                                                                      

 抽象类shape.class.php代码如下:

 1 <?php 2 /*       3         project:面向对象版图形计算器 4         file:shape.class.php 5         description:抽象类,定义两个抽象方法area()和perimeter(),以及定义方法validate对输入的值进行验证 6 */ 7 abstract class shape{ 8         public $shapeName;                      //形状名称; 9         abstract function area();               //抽象类area(),让子类去实现,体现多态性10         abstract function perimeter();          //抽象类perimeter();11 12         /*13                 @param mixed $value 接受表单输入值14                 @param string $message 提示消息前缀15                 @param boolean 返回值,成功为TRUE,失败为FALSE16         */17         protected function validate($value,$message = "输入的值"){18                 if($value < 0 || $value == NULL || !is_numeric($value)){19                         $message = $this->shapeName.$message;20                         echo '<font color="red">'.$message.'必须为正数</font><br>';21                         return FALSE;22                 }23                 else24                         return TRUE;25         }26 }

子类triangle.class.php代码如下:

 1 <?php 2 /** 3         project:面向对象版图形计算器 4         file:triangle.class.php 5         description:继承抽象类shape,计算并返回三角形的周长和面积 6 */ 7 class triangle extends shape{ 8         private $side1 = 0;             //边长1; 9         private $side2 = 0;             //边长2;10         private $side3 = 0;             //边长3;11 12         /*13                 构造函数:对表单变量进行合理性验证,通过则初始化三个边长14         */15         function __construct(){16                 $this->shapeName = "三角形";    //命名图形17 18                 //使用父类的方法validate检查输入的是否为正数19                 if($this->validate($_POST["side1"],"边长1") & $this->validate($_POST["side2"],"边长2") & $this->validate($_POST["side3"],"边长3")){20 21                         //使用私有方法验证两边和是否大于第三边22                         if($this->validatesum($_POST["side1"],$_POST["side2"],$_POST["side3"])){23                                 $this->side1 = $_POST["side1"];         //若通过验证初始化三边;24                                 $this->side2 = $_POST["side2"];25                                 $this->side3 = $_POST["side3"];26                         }27                         else{28                                 echo '<font color="red">两边的和要大于第三边</font>';29                                 exit();30                         }31                 }32                 else{33                         exit();34                 }35         }36         /*使用海伦公式计算面积,并返回结果*/37         function area(){38                 $s = ($_POST["side1"] + $_POST["side2"] + $_POST["side3"])/2;39                 return sqrt($s * ($s - $_POST["side1"]) * ($s - $_POST["side2"]) * ($s - $_POST["side3"]));40         }41         /*计算并返回周长*/42         function perimeter(){43                 return $_POST["side1"] + $_POST["side2"] + $_POST["side3"];44         }45         /*计算三角形两边和是否大于第三边,是返回TRUE,否返回FALSE*/46         private function validatesum($side1,$side2,$side3){47                 if(($side1 + $side2) > $side3 && ($side1 + $side3) > $side2 && ($side2 + $side3) > $side1)48                         return TRUE;49                 else50                         return FALSE;51         }52 }

子类circle.class.php代码如下:

 1 <?php 2 /* 3         project:面向对象的图形计算器 4         file:circle.class.php 5         description:接收表单值,返回周长和面积 6 */ 7 class circle extends shape{ 8         private $radius;        //圆的半径 9 10         //初始化圆的名称,检查输入合法性并初始化半径11         function __construct(){12                 $this->shapeName = "圆形";13                 if($this->validate($_POST["radius"],"半径"))14                         $this->radius = $_POST["radius"];15         }16         //返回圆的面积17         function area(){18                 return 3.14 * $this->radius * $this->radius;19         }20         //返回圆的周长21         function perimeter(){22                 return 3.14 * 2 * $this->radius;23         }24 }

子类rect.class.php代码如下:

 1 <?php 2 /* 3         project:面向对象的图形计算器 4         file:rect.class.php 5         descrition:声明一个矩形资料,实现形状抽象类计算周长和面积的方法,返回矩形的周长和面积 6 */ 7 class rect extends shape{ 8         private $width;         //矩形的宽度 9         private $height;        //矩形的高度10 11         //使用父类的validate方法验证输入的合法性,通过则初始化宽度和高度12         function __construct(){13         $this->shapeName = "矩形";14         if($this->validate($_POST["width"],"宽度") && $this->validate($_POST["height"],"高度")){15                 $this->width = $_POST["width"];16                 $this->height = $_POST["height"];17         }18         }19         //返回面积20         function area(){21                 return $this->width * $this->height;22         }23         //返回周长24         function perimeter(){25                 return 2 * ($this->width + $this->height);26         }27 }

 声明:

  1.本文只适合实验,不适合现实应用,若造成不良后果,本人概不负责。

  2.本文为原创博客,可以在个人平台自由转载,但需要注明出处,附上链接,否则视为盗用。严禁用于商业用途,如有需要,联系本人支付稿费,授权后方能使用。

相关文章列表:
关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助