主程序頁面 test.php頁面
形狀抽象類Shape.class.php頁面
<?php //形狀抽象類 abstract class Shape{ public $name; //面積 abstract function area(); //周長 abstract function circumference(); //圖形界面 abstract function view(); //形狀驗證方法 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="計算" /> <br/>'; $form .= '</form>'; echo $form; } //形狀驗證方法 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.'的第一個邊:<input type="text" name="edge1" value="'.$_POST['edge1'].'" /> <br/>'; $form .= $this->name.'的第二個邊:<input type="text" name="edge2" value="'.$_POST['edge2'].'" /> <br/>'; $form .= $this->name.'的第三個邊:<input type="text" name="edge3" value="'.$_POST['edge3'].'" /> <br/>'; $form .= '<input type="submit" name="dosubmit" value="計算" /> <br/>'; $form .= '</form>'; echo $form; } //形狀驗證方法 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; } }瀏覽器 矩形頁面
瀏覽器 三角形頁面
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
新聞標題:PHP對象多態(tài)性簡單圖形計算器高洛峰細說PHP-創(chuàng)新互聯(lián)
文章源于:http://chinadenli.net/article16/dicedg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、定制開發(fā)、搜索引擎優(yōu)化、企業(yè)網(wǎng)站制作、標簽優(yōu)化、響應式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容