はじめに
抽象メソッド/クラスについてまとめる
抽象メソッドとは
サブクラスでオーバーライドされることを期待/強制したメソッド。
また、抽象メソッドを含んだクラスのことを抽象クラスという。
参考コード
抽象メソッドを定義するには、abstract
修飾子を使う
抽象メソッドはサブクラスで必ずオーバーライドされるべきメソッドなので、スーパークラスの側では中身を持つことはできない。
Figure.php
abstract class Figure
{
protected float $width;
protected float $height;
public function __construct(float $width, float $height)
{
$this->width = $width;
$this->height = $height;
}
protected abstract function getArea(): float;
}