主程序:
<body>
<center>
<h1>图形计算器</h1>
<a href="new.php?action=ju">矩形</a> ||
<a href="new.php?action=san">三角</a> ||
<a href="new.php?action=yuan">圆形</a>
<hr>
</center>
<?php
function __autoload($ClassName){
include strtolower($ClassName).".class.php";
}
error_reporting(E_ALL&~E_NOTICE);
echo new Form();
//判断是否点击了提交按钮
if(isset($_POST['sub'])){
echo new Result();
}
?>
</body>
result类:
<?php
class Result{
private $shape;
function __construct(){
switch($_POST['action']){
case 'ju':
$this->shape=new Ju();
break;
case'san':
$this->shape=new San();
break;
case'yuan':
$this->shape=new Yuan();
break;
default:
$this->shape=false;
break;
}
}
function __toString(){
var_dump($this->shape->shapeName);
if($this->shape){
$result=$this->shape->ShapeName.'的周长'.$this->shape->perimeter().'<br>';
$result.=$this->shape->ShapeName.'的面积'.$this->shape->area().'<br>';
return $result;
}else{
return '没有这个形状';
}
}
}
ju类:
<?php
class Ju extends shape{
private $width=0;
private $height=0;
function __construct(){
$this->shapeName="矩形";
$this->width=$_POST['width'];
$this->height=$_POST['height'];
}
function area(){
return $this->width*$this->height;
}
function perimeter(){
return 2*($this->width+$this->height);
}
}
shape抽象类:
<?php
abstract class Shape{
public $shapeName;
abstract function area();
abstract function perimeter();
}
还有san类和 yuan类没写,和ju同理的,这是个图形计算器 ,可是输出结果是这样的
为什么result类中的$this->shape->shapeName 明明是字符串却不能输出出来
本来按我想的输出结果是 $this->shape->shapeName的面积 ,的周长,但是不知道为什么,var_dump打印出来是字符串。但是就是输出不出来
class Ju extends shape 中的shape要大写吧
在shap类中将shapname作为属性才能调用从而回去值