< ?PHP |
abstract class Shape { |
abstract protected function get_area(); |
//和一般的方法不同的是,这个方法没有大括号 |
//你不能创建这个抽象类的实例:$Shape_Rect= new Shape(); |
} |
class Rectangle extends Shape{ |
private $width ; |
private $height ; |
function __construct( $width =0, |
$height =0){ |
$this ->width= $width ; |
$this ->height= $height ; |
} |
function get_area(){ |
echo ( $this ->width+ $this ->height)*2; |
} |
} |
$Shape_Rect = new Rectangle(20,30); |
$Shape_Rect ->get_area(); |
?> |