首页 > 后端开发 > php教程 > 使用面向对象的图形计算器

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

WBOY
发布: 2016-08-08 09:22:43
原创
1495 人浏览过

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

  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代码如下:

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

form.class.php代码如下:

<span> 1</span> <span>php
</span><span> 2</span><span>/*</span><span> 3</span><span>        project:面向对象版图形计算器
</span><span> 4</span><span>        file:form.class.php
</span><span> 5</span><span>        description:对不同的图形输出不同的表单
</span><span> 6</span><span>*/</span><span> 7</span><span>class</span><span> form{
</span><span> 8</span><span>private</span><span>$formAction</span>=<span>NULL</span>;       <span>//</span><span>保存响应表单的文件</span><span> 9</span><span>private</span><span>$shape</span>=<span>NULL</span>;            <span>//</span><span>保存图形的形状</span><span>10</span><span>11</span><span>/*</span><span>12</span><span>                @param string $action 对象初始化传入的参数,代表响应的页面的是哪一个文件
</span><span>13</span><span>*/</span><span>14</span><span>function</span> __construct(<span>$action</span> = ""<span>){
</span><span>15</span><span>$this</span>->formAction = <span>$action</span>;    <span>//</span><span>把传入的参数保存到$formAction中;</span><span>16</span><span>$this</span>->shape = <span>isset</span>(<span>$_GET</span>["shape"]) ? <span>$_GET</span>["shape"]:"rect";   <span>//</span><span>从表单传递的变量中获取图形类别,如没有传递,默认为矩形</span><span>17</span><span>                }
</span><span>18</span><span>function</span><span> __toString(){
</span><span>19</span><span>$form</span> = '
登录后复制
'; 20//下面两行使用变量函数调用对应图形的私有函数,返回input部分表单的字符串21$shape = 'get'.ucfirst($this->shape); 22$form .= $this->$shape(); 2324$form .= ''; 25$form .= '
'; 2627return$form; 28 } 29//私有方法,返回矩形表单input部分的字符串;30privatefunction getRect(){ 31//在表单提交后输入的内容继续显示,防止其消失32$formheight=isset($_POST['height']) ? $_POST['height'] : NULL; 33$formwidth=isset($_POST['width']) ? $_POST['width'] : NULL; 34$input = '

请输入矩形的长和宽

'; 35$input .= '矩形的高度:
'; 36$input .= '矩形的宽度:'; 37return$input; 38 } 39//返回三角形输入表单input部分的字符串40privatefunction 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 = '

请输入三角形的三边

'; 46$input .= '边长1:'; 47$input .= '边长2:'; 48$input .= '边长3:'; 49return$input; 50 } 51//返回圆形表单input部分的字符串52privatefunction getCircle(){ 53$formradius=isset($_POST['radius']) ? $_POST['radius'] : NULL; //在输入表单提交后内容继续显示出来,防止其消失54$input = '

请输入半径

'; 55$input .= '半径:'; 56return$input; 57 } 58 } 59

 result.class.php代码如下:

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

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

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

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

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

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

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

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

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

 声明:

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

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

以上就介绍了使用面向对象的图形计算器,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板