PHP對(duì)象多態(tài)性簡單圖形計(jì)算器高洛峰細(xì)說PHP-創(chuàng)新互聯(lián)

主程序頁面 test.php頁面

我們提供的服務(wù)有:做網(wǎng)站、成都網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、襄州ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的襄州網(wǎng)站制作公司<!DOCTYPE html> <html> <head>     <title>簡單的圖形計(jì)算器</title>     <meta http-equiv = "Content-Type" content = "text/html;charset=utf-8" /> </head> <body>     <center>     <h2>簡單的圖形計(jì)算器</h2>     <a href = "test.php?action=rectangle">矩形</a>&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;     <a href = "test.php?action=triangle">三角形</a>     </center>     <br/><hr>     <?php     //屏蔽E_NOTICE提示     error_reporting(E_ALL & ~E_NOTICE);     //設(shè)置自動(dòng)加載這個(gè)程序需要的類文件     function __autoload($classname){         include $classname.'.class.php';     }     //判斷用戶是否單擊一個(gè)形狀鏈接     if(!empty($_GET['action'])){         //第一步:創(chuàng)建形狀的對(duì)象         $classname = ucfirst($_GET['action']);         $shape = new $classname($_POST);         //第二步:調(diào)用形狀的對(duì)象中的圖形界面         $shape->view();         //第三步:用戶是否提交了對(duì)應(yīng)的圖形界面的表單         if(isset($_POST['dosubmit'])){             //第四步:查看用戶輸入的數(shù)據(jù)是否合法,不合法則提示             if($shape->validate($_POST)){             //第五步:計(jì)算圖形的面積和周長             echo $shape->name.'的面積為:'.$shape->area().'<br/>';             echo $shape->name.'的周長為:'.$shape->circumference().'<br/>';             }         }             }else{//如果用戶沒有單擊則默認(rèn)訪問主程序         echo '請(qǐng)選擇一個(gè)要計(jì)算的圖形';     }     ?> </body> </html>

形狀抽象類Shape.class.php頁面

<?php   //形狀抽象類 abstract class Shape{         public  $name;         //面積         abstract function area();         //周長         abstract function circumference();         //圖形界面         abstract function view();         //形狀驗(yàn)證方法         abstract function validate($arr);          }

矩形類Rectangle.class.php頁面

<?php  //矩形類 class Rectangle extends Shape{     private $width;     private $height;     function __construct($arr=array()){         if(!empty($arr)){         $this->width   =  $arr['width'];         $this->height  =  $arr['height'];         }         $this->name   =  '矩形';     }      function area(){         return $this->width*$this->height;     }     //周長     function circumference(){         return 2*($this->width+$this->height);     }     //圖形界面   function view(){         $form =  '<form action="test.php?action=rectangle" method="post">';         $form .= $this->name.'的寬:<input type="text" name="width" value="'.$_POST['width'].'" /> <br/>';          $form .= $this->name.'的高:<input type="text" name="height" value="'.$_POST['height'].'" /> <br/>';          $form .= '<input type="submit" name="dosubmit" value="計(jì)算" /> <br/>';          $form .= '</form>';         echo $form;     }     //形狀驗(yàn)證方法      function validate($arr){         $flag = true;         if($arr['width']<0 || !is_numeric($arr['width'])){             echo $this->name.'的寬必須是大于0的整數(shù)<br/>';             $flag = false;         }         if($arr['height']<0 || !is_numeric($arr['height'])){             echo $this->name.'的高必須是大于0的整數(shù)<br/>';             $flag = false;         }         return $flag;     }      }

三角形類Triangle.class.php頁面

<?php  //三角形類 class Triangle extends Shape{     private $edge1;     private $edge2;     private $edge3;     function __construct($arr=array()){         if(!empty($arr)){         $this->edge1   =  $arr['edge1'];         $this->edge2   =  $arr['edge2'];         $this->edge3   =  $arr['edge3'];                 }         $this->name   =  '三角形';     }      function area(){          $p =($this->edge1+$this->edge2+$this->edge3)/2;                  return sqrt($p*($p-$this->edge1)*($p-$this->edge2)*($p-$this->edge3));     }     //周長     function circumference(){         return ($this->edge1+$this->edge2+$this->edge3);     }     //圖形界面   function view(){         $form =  '<form action="test.php?action=triangle" method="post">';         $form .= $this->name.'的第一個(gè)邊:<input type="text" name="edge1" value="'.$_POST['edge1'].'" /> <br/>';          $form .= $this->name.'的第二個(gè)邊:<input type="text" name="edge2" value="'.$_POST['edge2'].'" /> <br/>';          $form .= $this->name.'的第三個(gè)邊:<input type="text" name="edge3" value="'.$_POST['edge3'].'" /> <br/>';          $form .= '<input type="submit" name="dosubmit" value="計(jì)算" /> <br/>';          $form .= '</form>';         echo $form;     }     //形狀驗(yàn)證方法      function validate($arr){         $flag = true;         if($arr['edge1']<0 || !is_numeric($arr['edge1'])){             echo $this->name.'的第一邊必須是大于0的整數(shù)<br/>';             $flag = false;         }         if($arr['edge2']<0 || !is_numeric($arr['edge2'])){             echo $this->name.'的第二邊必須是大于0的整數(shù)<br/>';             $flag = false;         }         if($arr['edge3']<0 || !is_numeric($arr['edge3'])){             echo $this->name.'的第三邊必須是大于0的整數(shù)<br/>';             $flag = false;         }         if(($arr['edge1']+$arr['edge2']<$arr['edge3']) || ($arr['edge1']+$arr['edge3']<$arr['edge2'])||($arr['edge3']+$arr['edge2']<$arr['edge1']) ){             echo '三角形定義必須兩邊之和大于第三邊<br/>';             $flag = false;         }         return $flag;     }      }

瀏覽器 矩形頁面

PHP 對(duì)象  多態(tài)性 簡單圖形計(jì)算器 高洛峰 細(xì)說PHP

瀏覽器 三角形頁面

PHP 對(duì)象  多態(tài)性 簡單圖形計(jì)算器 高洛峰 細(xì)說PHP

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

新聞標(biāo)題:PHP對(duì)象多態(tài)性簡單圖形計(jì)算器高洛峰細(xì)說PHP-創(chuàng)新互聯(lián)
文章源于:http://bm7419.com/article16/dicedg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、定制開發(fā)、搜索引擎優(yōu)化企業(yè)網(wǎng)站制作、標(biāo)簽優(yōu)化、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管