使用物件導向的圖形計算器

WBOY
發布: 2016-08-08 09:22:43
原創
1455 人瀏覽過

這個例子可能並不實用,但基本上概括了物件導向的三個特徵:繼承性,封裝性,多態性。本例的主要功能有:

  1. 讓使用者可以選擇不同類型的圖形;
  2. 對所選的圖形輸入其相關屬性;
  3. 根據輸入的屬性計算圖形的周長和麵積。

效果如下:

A:

    1. A部分就直接在index.思路中寫,點擊對應,使用了自動載入類別。
    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> <html>
<span> 2</span> <head>
<span> 3</span>         <meta http-equiv="charset" c> 4</span> </head>
<span> 5</span> <body>
<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> </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.phpfo

<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> }                                                                                                                                      
登入後複製

子類triangle.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> }
登入後複製

子類circle.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> }
登入後複製

聲明:

  1.本文只適合實驗,不適合現實應用,若造成不良後果,本人概不負責。

  2.本文為原創博客,可以在個人平台自由轉載,但需要註明出處,附上鏈接,否則視為盜用。嚴禁用於商業用途,如有需要,聯絡本人支付稿費,授權後方可使用。

以上就介紹了使用物件導向的圖形計算器,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板