Heim >Backend-Entwicklung >PHP-Tutorial >使用面向对象的图形计算器,面向对象图形计算器_PHP教程
这个例子可能并不实用,但基本概括了面向对象的三个特征:继承性,封装性,多态性。本例的主要功能有:
效果如下:
思路:
需要改进的地方:
index.php代码如下:
<span> 1</span> <html> <span> 2</span> <head> <span> 3</span> <meta http-equiv="charset" content="utf-8"> <span> 4</span> </head> <span> 5</span> <body> <span> 6</span> <div id="center"> <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 id="inputForm"> <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> </body> <span>34</span> </html>
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> = '<form action="'.<span>$this</span>->formAction.'?shape='.<span>$this</span>->shape.'" method="post">'<span>; </span><span>20</span> <span>//</span><span>下面两行使用变量函数调用对应图形的私有函数,返回input部分表单的字符串</span> <span>21</span> <span>$shape</span> = 'get'.<span>ucfirst</span>(<span>$this</span>-><span>shape); </span><span>22</span> <span>$form</span> .= <span>$this</span>-><span>$shape</span><span>(); </span><span>23</span> <span>24</span> <span>$form</span> .= '</br><input type="submit" name="sub" value="计算"/></br>'<span>; </span><span>25</span> <span>$form</span> .= '</form>'<span>; </span><span>26</span> <span>27</span> <span>return</span> <span>$form</span><span>; </span><span>28</span> <span> } </span><span>29</span> <span>//</span><span>私有方法,返回矩形表单input部分的字符串;</span> <span>30</span> <span>private</span> <span>function</span><span> getRect(){ </span><span>31</span> <span>//</span><span>在表单提交后输入的内容继续显示,防止其消失</span> <span>32</span> <span>$formheight</span>=<span>isset</span>(<span>$_POST</span>['height']) ? <span>$_POST</span>['height'] : <span>NULL</span><span>; </span><span>33</span> <span>$formwidth</span>=<span>isset</span>(<span>$_POST</span>['width']) ? <span>$_POST</span>['width'] : <span>NULL</span><span>; </span><span>34</span> <span>$input</span> = '<p>请输入矩形的长和宽</p>'<span>; </span><span>35</span> <span>$input</span> .= '矩形的高度:<input type="text" name="height" value="'.<span>$formheight</span>.'"/><br></br>'<span>; </span><span>36</span> <span>$input</span> .= '矩形的宽度:<input type="text" name="width" value="'.<span>$formwidth</span>.'"/></br>'<span>; </span><span>37</span> <span>return</span> <span>$input</span><span>; </span><span>38</span> <span> } </span><span>39</span> <span>//</span><span>返回三角形输入表单input部分的字符串</span> <span>40</span> <span>private</span> <span>function</span><span> getTriangle(){ </span><span>41</span> <span>//</span><span>在表单提交后继续显示出来,防止其消失</span> <span>42</span> <span>$formside1</span>=<span>isset</span>(<span>$_POST</span>['side1']) ? <span>$_POST</span>['side1'] : <span>NULL</span><span>; </span><span>43</span> <span>$formside2</span>=<span>isset</span>(<span>$_POST</span>['side2']) ? <span>$_POST</span>['side2'] : <span>NULL</span><span>; </span><span>44</span> <span>$formside3</span>=<span>isset</span>(<span>$_POST</span>['side3']) ? <span>$_POST</span>['side3'] : <span>NULL</span><span>; </span><span>45</span> <span>$input</span> = '<p>请输入三角形的三边</p>'<span>; </span><span>46</span> <span>$input</span> .= '边长1:<input type="text" name="side1" value="'.<span>$formside1</span>.'" /></br></br>'<span>; </span><span>47</span> <span>$input</span> .= '边长2:<input type="text" name="side2" value="'.<span>$formside2</span>.'"/></br></br>'<span>; </span><span>48</span> <span>$input</span> .= '边长3:<input type="text" name="side3" value="'.<span>$formside3</span>.'"/></br>'<span>; </span><span>49</span> <span>return</span> <span>$input</span><span>; </span><span>50</span> <span> } </span><span>51</span> <span>//</span><span>返回圆形表单input部分的字符串</span> <span>52</span> <span>private</span> <span>function</span><span> getCircle(){ </span><span>53</span> <span>$formradius</span>=<span>isset</span>(<span>$_POST</span>['radius']) ? <span>$_POST</span>['radius'] : <span>NULL</span>; <span>//</span><span>在输入表单提交后内容继续显示出来,防止其消失</span> <span>54</span> <span>$input</span> = '<p>请输入半径</p>'<span>; </span><span>55</span> <span>$input</span> .= '半径:<input type="text" name="radius" value="'.<span>$formradius</span>.'"/></br>'<span>; </span><span>56</span> <span>return</span> <span>$input</span><span>; </span><span>57</span> <span> } </span><span>58</span> <span> } </span><span>59</span>
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().'</br>'<span>; </span><span>12</span> <span>$result</span> .= <span>$this</span>->shape->shapeName.'的面积为'.<span>$this</span>->shape->area().'</br>'<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> < 0 || <span>$value</span> == <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.本文为原创博客,可以在个人平台自由转载,但需要注明出处,附上链接,否则视为盗用。严禁用于商业用途,如有需要,联系本人支付稿费,授权后方能使用。