PHP学习笔记06——面向对象版图形计算器
index.php 用于显示页面
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <title>图形计算器</title> 5 </head> 6 <body> 7 <center> 8 <h1>图形(周长&面积)计算器</h1> 9 <a href = "index.php?action=rect">矩形</a> 10 <a href = "index.php?action=circle">圆形</a> 11 </center> 12 <?php 13 //错误级别设置为除了注意全部输出,避免输出变量未定义等消息 14 error_reporting(E_ALL & ~E_NOTICE); 15 16 //使用自动加载类技术 17 function __autoload($className){ 18 include strtolower($className).".class.php"; 19 } 20 21 //实际是调用了Form类的__toString方法; 22 echo new Form("index.php"); 23 24 if(isset($_POST["sub"])) { 25 echo new Result(); 26 } 27 ?> 28 </body> 29 30 31 </html>
form.class.php 根据action不同(rect,circle)显示不同的form
1 <?php 2 class Form { 3 private $action; 4 private $shape; 5 6 function __construct($action = "") { 7 $this->action = $action; 8 //默认rect; 9 $this->shape = isset($_GET['action']) ? $_GET['action'] : "rect"; 10 } 11 12 function __toString() { 13 $form = '<form action = "'.$this->action.'?action='.$this->shape.'" method="post">'; 14 15 //根据get请求组成方法名称字符串,例如GetRect() 16 $shape = "get".ucfirst($this->shape); 17 $form .= $this->$shape(); 18 $form .= '<br/><input type="submit" name="sub" value="计算"><br/>'; 19 $form .= '</form>'; 20 return $form; 21 } 22 23 private function getRect() { 24 $input = '<b>请输入 | 矩形 | 的宽度和高度: </b><p>'; 25 $input .= '宽度: <input type="text" name="width" value="'.$_POST["width"].'"/><br/>'; 26 $input .= '高度: <input type="text" name="height" value="'.$_POST["height"].'"/><br/>'; 27 return $input; 28 } 29 30 private function getCircle() { 31 $input = '<b>请输入 | 圆形 | 的半径: </b><p>'; 32 $input .= '半径: <input type="text" name="radius" value="'.$_POST["radius"].'"/><br/>'; 33 return $input; 34 } 35 36 } 37 ?>
result.class.php 根据action构造对应的类并输出结果
1 <?php 2 class Result { 3 private $shape = null; 4 //构造方法,根据action类新建类 5 function __construct(){ 6 $this->shape = new $_GET['action'](); 7 } 8 //利用多态性根据形状自动计算周长和面积 9 function __toString(){ 10 $result = $this->shape->shapeName.'的周长: '.round($this->shape->perimeter(), 2).'<br/>'; 11 $result .= $this->shape->shapeName.'的面积: '.round($this->shape->area(), 2).'<br/>'; 12 return $result; 13 } 14 } 15 16 ?>
shape.class.php 图形的基类,定义了计算面积和周长的抽象方法
1 <?php 2 abstract class Shape { 3 public $shapeName; 4 //定义两个抽象方法,计算面积和周长 5 abstract function area(); 6 abstract function perimeter(); 7 8 //验证数据合法性的方法 9 protected function validate($value, $messgae = "输入值") { 10 if ($value==""||!is_numeric($value)||$value < 0) { 11 $messgae = $this->shapeName.$messgae; 12 echo '<font colr="red">'.$messgae.'不合法</font><br/>'; 13 return false; 14 } 15 return true; 16 } 17 } 18 19 ?>
rect.class.php 矩形的实现
1 <?php 2 class Rect extends Shape { 3 private $width = 0; 4 private $height = 0; 5 function __construct(){ 6 $this->shapeName = "矩形"; 7 if ($this->validate($_POST["width"], "高度")&&$this->validate($_POST["height"], "宽度")) { 8 $this->width = $_POST["width"]; 9 $this->height = $_POST["height"]; 10 } 11 } 12 13 public function area() { 14 return $this->width * $this->height; 15 } 16 public function perimeter() { 17 return 2 * ($this->width + $this->height); 18 } 19 } 20 ?>
circle.class.php 圆形的实现
1 <?php 2 class Circle extends Shape { 3 private $radius = 0; 4 function __construct(){ 5 $this->shapeName = "圆形"; 6 if ($this->validate($_POST["radius"], "半径")) { 7 $this->radius = $_POST["radius"]; 8 } 9 } 10 11 public function area() { 12 return pi() * $this->radius * $this->radius; 13 } 14 public function perimeter() { 15 return 2 * pi() * $this->radius; 16 } 17 } 18 ?>
页面结果