什么是抽象类?
<?php
abstract class AbstractClass
{
// 强制要求子类定义这些方法
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// 普通方法(非抽象方法)
public function printOut() {
print $this->getValue() . "\n";
}
}
抽象类示例
<?php
abstract class Shape
{
abstract protected function getArea();
}
class Rectangle extends Shape
{
protected $length = 4;
public function getArea()
{
return $this->length * $this->length;
}
}
$re = new Rectangle();
var_dump($re->getArea());
总结
- 抽象类不能实例化
- (非抽象的)子类必须实现抽象方法,从而保证代码的统一性