主程序页面 test.php页面
简单的图形计算器 简单的图形计算器
矩形 || 三角形
view(); //第三步:用户是否提交了对应的图形界面的表单 if(isset($_POST['dosubmit'])){ //第四步:查看用户输入的数据是否合法,不合法则提示 if($shape->validate($_POST)){ //第五步:计算图形的面积和周长 echo $shape->name.'的面积为:'.$shape->area().'
'; echo $shape->name.'的周长为:'.$shape->circumference().'
'; } } }else{//如果用户没有单击则默认访问主程序 echo '请选择一个要计算的图形'; } ?>
形状抽象类Shape.class.php页面
'; echo $form; } //形状验证方法 function validate($arr){ $flag = true; if($arr['width']<0 || !is_numeric($arr['width'])){ echo $this->name.'的宽必须是大于0的整数矩形类Rectangle.class.php页面
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 = '
三角形类Triangle.class.php页面
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 = '';
echo $form;
}
//形状验证方法
function validate($arr){
$flag = true;
if($arr['edge1']<0 || !is_numeric($arr['edge1'])){
echo $this->name.'的第一边必须是大于0的整数浏览器 矩形页面

浏览器 三角形页面

上一篇:PHP内部声明的类和接口
下一篇:PHP 对象 多态性