温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP 对象 多态性 简单图形计算器 高洛峰 细说PHP

发布时间:2020-07-21 20:21:43 来源:网络 阅读:545 作者:津沙港湾 栏目:web开发

主程序页面 test.php页面

<!DOCTYPE html>
<html>
<head>
    <title>简单的图形计算器</title>
    <meta http-equiv = "Content-Type" content = "text/html;charset=utf-8" />
</head>
<body>
    <center>
    <h2>简单的图形计算器</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);
    //设置自动加载这个程序需要的类文件
    function __autoload($classname){
        include $classname.'.class.php';
    }
    //判断用户是否单击一个形状链接
    if(!empty($_GET['action'])){
        //第一步:创建形状的对象
        $classname = ucfirst($_GET['action']);
        $shape = new $classname($_POST);
        //第二步:调用形状的对象中的图形界面
        $shape->view();
        //第三步:用户是否提交了对应的图形界面的表单
        if(isset($_POST['dosubmit'])){
            //第四步:查看用户输入的数据是否合法,不合法则提示
            if($shape->validate($_POST)){
            //第五步:计算图形的面积和周长
            echo $shape->name.'的面积为:'.$shape->area().'<br/>';
            echo $shape->name.'的周长为:'.$shape->circumference().'<br/>';
            }
        }        
    }else{//如果用户没有单击则默认访问主程序
        echo '请选择一个要计算的图形';
    }
    ?>
</body>
</html>

形状抽象类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的整数<br/>';
            $flag = false;
        }
        if($arr['height']<0 || !is_numeric($arr['height'])){
            echo $this->name.'的高必须是大于0的整数<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的整数<br/>';
            $flag = false;
        }
        if($arr['edge2']<0 || !is_numeric($arr['edge2'])){
            echo $this->name.'的第二边必须是大于0的整数<br/>';
            $flag = false;
        }
        if($arr['edge3']<0 || !is_numeric($arr['edge3'])){
            echo $this->name.'的第三边必须是大于0的整数<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 对象  多态性 简单图形计算器 高洛峰 细说PHP

浏览器 三角形页面

PHP 对象  多态性 简单图形计算器 高洛峰 细说PHP

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI