ITPub博客

首页 > IT职业 > IT生活 > PHP 对象 多态性 简单图形计算器

PHP 对象 多态性 简单图形计算器

IT生活 作者:cnaka 时间:2021-09-09 09:16:41 0 删除 编辑

主程序页面 test.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().'
';

            echo $shape->name.'的周长为:'.$shape->circumference().'
';

            }

        }        

    }else{//如果用户没有单击则默认访问主程序

        echo '请选择一个要计算的图形';

    }

    ?>


形状抽象类Shape.class.php页面

 //形状抽象类

abstract class Shape{

        public  $name;

        //面积

        abstract function area();

        //周长

        abstract function circumference();

        //图形界面

        abstract function view();

        //形状验证方法

        abstract function validate($arr);         

}


矩形类Rectangle.class.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 .= $this->name.'的宽:
'; 

        $form .= $this->name.'的高:
'; 

        $form .= '
'; 

        $form .= '

';

        echo $form;

    }

    //形状验证方法

     function validate($arr){

        $flag = true;

        if($arr['width']

            echo $this->name.'的宽必须是大于0的整数
';

            $flag = false;

        }

        if($arr['height']

            echo $this->name.'的高必须是大于0的整数
';

            $flag = false;

        }

        return $flag;

    }}


三角形类Triangle.class.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 .= $this->name.'的第一个边:
'; 

        $form .= $this->name.'的第二个边:
'; 

        $form .= $this->name.'的第三个边:
'; 

        $form .= '
'; 

        $form .= '

';

        echo $form;

    }

    //形状验证方法

     function validate($arr){

        $flag = true;

        if($arr['edge1']

            echo $this->name.'的第一边必须是大于0的整数
';

            $flag = false;

        }

        if($arr['edge2']

            echo $this->name.'的第二边必须是大于0的整数
';

            $flag = false;

        }

        if($arr['edge3']

            echo $this->name.'的第三边必须是大于0的整数
';

            $flag = false;

        }

        if(($arr['edge1']+$arr['edge2']

            echo '三角形定义必须两边之和大于第三边
';

            $flag = false;

        }

        return $flag;

    }     

}


浏览器 矩形页面

图片描述

浏览器 三角形页面

图片描述

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3368/viewspace-2801684/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 高三的结束
下一篇: appserv升级php版本
请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    79
  • 访问量
    754