PHP学习笔记06——面向对象版图形计算器

index.php  用于显示页面

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>图形计算器</title>
</head>
<body>
<center>
<h1>图形(周长&面积)计算器</h1>
<a href = "index.php?action=rect">矩形</a>
<a href = "index.php?action=circle">圆形</a>
</center>
<?php
//错误级别设置为除了注意全部输出,避免输出变量未定义等消息
error_reporting(E_ALL & ~E_NOTICE); //使用自动加载类技术
function __autoload($className){
include strtolower($className).".class.php";
} //实际是调用了Form类的__toString方法;
echo new Form("index.php"); if(isset($_POST["sub"])) {
echo new Result();
}
?>
</body> </html>

form.class.php  根据action不同(rect,circle)显示不同的form

 <?php
class Form {
private $action;
private $shape; function __construct($action = "") {
$this->action = $action;
//默认rect;
$this->shape = isset($_GET['action']) ? $_GET['action'] : "rect";
} function __toString() {
$form = '<form action = "'.$this->action.'?action='.$this->shape.'" method="post">'; //根据get请求组成方法名称字符串,例如GetRect()
$shape = "get".ucfirst($this->shape);
$form .= $this->$shape();
$form .= '<br/><input type="submit" name="sub" value="计算"><br/>';
$form .= '</form>';
return $form;
} private function getRect() {
$input = '<b>请输入 | 矩形 | 的宽度和高度: </b><p>';
$input .= '宽度: <input type="text" name="width" value="'.$_POST["width"].'"/><br/>';
$input .= '高度: <input type="text" name="height" value="'.$_POST["height"].'"/><br/>';
return $input;
} private function getCircle() {
$input = '<b>请输入 | 圆形 | 的半径: </b><p>';
$input .= '半径: <input type="text" name="radius" value="'.$_POST["radius"].'"/><br/>';
return $input;
} }
?>

result.class.php  根据action构造对应的类并输出结果

 <?php
class Result {
private $shape = null;
//构造方法,根据action类新建类
function __construct(){
$this->shape = new $_GET['action']();
}
//利用多态性根据形状自动计算周长和面积
function __toString(){
$result = $this->shape->shapeName.'的周长: '.round($this->shape->perimeter(), 2).'<br/>';
$result .= $this->shape->shapeName.'的面积: '.round($this->shape->area(), 2).'<br/>';
return $result;
}
} ?>

shape.class.php  图形的基类,定义了计算面积和周长的抽象方法

 <?php
abstract class Shape {
public $shapeName;
//定义两个抽象方法,计算面积和周长
abstract function area();
abstract function perimeter(); //验证数据合法性的方法
protected function validate($value, $messgae = "输入值") {
if ($value==""||!is_numeric($value)||$value < 0) {
$messgae = $this->shapeName.$messgae;
echo '<font colr="red">'.$messgae.'不合法</font><br/>';
return false;
}
return true;
}
} ?>

rect.class.php  矩形的实现

 <?php
class Rect extends Shape {
private $width = 0;
private $height = 0;
function __construct(){
$this->shapeName = "矩形";
if ($this->validate($_POST["width"], "高度")&&$this->validate($_POST["height"], "宽度")) {
$this->width = $_POST["width"];
$this->height = $_POST["height"];
}
} public function area() {
return $this->width * $this->height;
}
public function perimeter() {
return 2 * ($this->width + $this->height);
}
}
?>

circle.class.php  圆形的实现

 <?php
class Circle extends Shape {
private $radius = 0;
function __construct(){
$this->shapeName = "圆形";
if ($this->validate($_POST["radius"], "半径")) {
$this->radius = $_POST["radius"];
}
} public function area() {
return pi() * $this->radius * $this->radius;
}
public function perimeter() {
return 2 * pi() * $this->radius;
}
}
?>

页面结果

PHP学习笔记06——面向对象版图形计算器

PHP学习笔记06——面向对象版图形计算器

上一篇:[dx11]利用SpriteFont绘制中文--本地化文本


下一篇:UIwebView 和 H5交互详情