<?php abstract class Shape { abstract function getArea(); } abstract class Polygon extends Shape { abstract function getNumSides(); } class Triangle extends Polygon { public $base; public $height; public function __construct($base, $height){ $this->base = $base; $this->height = $height; } public function getArea(){ return (($this->base*$this->height)/2); } public function getNumSides(){ return (3); } } class Rectangle extends Polygon { protected $width; protected $height; public function __construct($width, $height){ $this->width = $width; $this->height = $height; } public function getArea(){ return ($this->width*$this->height); } public function getNumSides(){ return 4; } } class Circle extends Shape { protected $radius; public function __construct($radius){ $this->radius = $radius; $this->color = $color; } public function getArea(){ return ($this->radius*$this->radius); } } $a = new Triangle(8,10); $b = new Rectangle(4,6); $c = new Circle(5); $collection = array($a,$b,$c); //print_r($collection); foreach($collection as $s){ if($s instanceof Shape){ echo '面积为:'.$s->getArea().'<br />'; } if($s instanceof Polygon){ echo '边数为:'.$s->getNumSides().'<br />'; } } ?> 用来求面积的