主程序頁(yè)面 test.php頁(yè)面
形狀抽象類Shape.class.php頁(yè)面
<?php //形狀抽象類 abstract class Shape{ public $name; //面積 abstract function area(); //周長(zhǎng) abstract function circumference(); //圖形界面 abstract function view(); //形狀驗(yàn)證方法 abstract function validate($arr); }矩形類Rectangle.class.php頁(yè)面
<?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; } //周長(zhǎng) 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頁(yè)面
<?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)); } //周長(zhǎng) 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; } }瀏覽器 矩形頁(yè)面
瀏覽器 三角形頁(yè)面
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
新聞標(biāo)題:PHP對(duì)象多態(tài)性簡(jiǎn)單圖形計(jì)算器高洛峰細(xì)說PHP-創(chuàng)新互聯(lián)
文章源于:http://chinadenli.net/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)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容