本文使用面向对象的思想,做了一个图形计算器的小例子:
1、架构图
2、说明
(1)index.php页面只需要直接调用Form类和Result类
(2)Form类用于输入一个表单
(3)为了实现多态的特性,Result类用于操作数据
3、Shape类
<?php
abstract class Shape{
abstract function area();//面积
abstract function perimeter();//周长
}
4、Rectangle类
<?php
class Rectangle extends Shape {
private $width=0;
private $height=0;
function __construct($width,$height){
$this->width=$width;
$this->height=$height;
}
function area(){
return $this->width * $this->height;
}
function perimeter(){
return 2*$this->width + 2*$this->height;
}
}
5、Triangle类
<?php
class Triangle extends Shape {
private $side1=0;
private $side2=0;
private $side3=0;
function __construct($side1,$side2,$side3){
$this->side1=$side1;
$this->side2=$side2;
$this->side3=$side3;
}
function area(){
if ($this->isTriangle()){
$temp=($this->side1 + $this->side2 + $this->side3)/2;
return sqrt($temp * ($temp - $this->side1) * ($temp - $this->side2) * ($temp - $this->side3) );
}else{
return "无法构成三角形";
}
}
function perimeter(){
if ($this->isTriangle()){
return $this->side1 + $this->side2 + $this->side3;
}else{
return "无法构成三角形";
}
}
//需要验证是否能构成三角形
function isTriangle(){
if (($this->side1 + $this->side2 > $this->side3)&&($this->side1 + $this->side3 > $this->side2)&&($this->side2 + $this->side3 > $this->side1)){
return true;
}else{
return false;
}
}
}
6、Circle类
<?php
class Circle extends Shape {
private $radius=0;
function __construct($radius){
$this->radius=$radius;
}
function area(){
return M_PI * $this->radius * $this->radius;
}
function perimeter(){
return 2 * M_PI * $this->radius;
}
}
7、Result类
<?php
class Result{
private $shape=null;//形状
private $result='';//结果
//通过不同的名字,创建不同的对象
function __construct(){
switch ($_POST["subForm"]){
case "rectangle":
$this->shape=new Rectangle($_POST["width"],$_POST["height"]);
break;
case "triangle":
$this->shape=new Triangle($_POST["side1"],$_POST["side2"],$_POST["side3"]);
break;
case "circle":
$this->shape=new Circle($_POST["radius"]);
break;
default:
$this->shape=new Rectangle($_POST["width"],$_POST["height"]);
}
}
function __toString(){
$this->result.='面积是:'.$this->shape->area();
$this->result.='周长是:'.$this->shape->perimeter();
return $this->result;
}
}
8、Form类
<?php
class Form{
private $index;//位置
function __construct($index="index.php"){
$this->index=$index;
}
function __toString(){
$form='<form action="'.$this->index .'?action='. $_GET["action"] .'" method="post">';
switch ($_GET["action"]){
case "rectangle":
$form.=$this->getRectangle();
break;
case "triangle":
$form.=$this->getTriangle();
break;
case "circle":
$form.=$this->getCircle();
break;
default:
$form.=$this->getRectangle();
}
$form.='<input type="text" hidden="hidden" name="subForm" value="'.$_GET["action"].'">';
$form.='<input type="submit" value="计算">';
$form.='</form>';
return $form;
}
function getRectangle(){
$rectangle='矩形:<br>';
$rectangle.='宽度:<input type="text" name="width">'.'<br>';
$rectangle.='高度:<input type="text" name="height">'.'<br>';
return $rectangle;
}
function getTriangle(){
$rectangle='三角形:<br>';
$rectangle.='边一:<input type="text" name="side1">'.'<br>';
$rectangle.='边二:<input type="text" name="side2">'.'<br>';
$rectangle.='边三:<input type="text" name="side3">'.'<br>';
return $rectangle;
}
function getCircle(){
$rectangle='圆形:<br>';
$rectangle.='半径:<input type="text" name="radius">'.'<br>';
return $rectangle;
}
}
9、index.php
<html lang="cn">
<head>
<meta charset="UTF-8">
<title>图形计算</title>
</head>
<body>
<center>
<h1>计算图形</h1>
<a href="index.php?action=rectangle">矩形</a>
<a href="index.php?action=triangle">三角形</a>
<a href="index.php?action=circle">圆形</a>
</center>
<?php
// error_reporting(E_ERROR);
// ini_set("display_errors","Off"); //屏蔽掉PHP警告和错误提示
//类的自动加载
function __autoload($className){
include strtolower($className).".class.php";
}
//显示表单
echo new Form();
echo '<br><br><br><br>结果:<br>';
//操作数据
echo new Result();
?>
</body>
</html>
更多推荐
PHP入门-使用面向对象思想开发的图形计算器
发布评论