復制代碼 代碼如下:
< ?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();
?>
新聞熱點
疑難解答