0
0

抽象クラス

Posted at

はじめに

抽象メソッド/クラスについてまとめる

抽象メソッドとは

サブクラスでオーバーライドされることを期待/強制したメソッド。
また、抽象メソッドを含んだクラスのことを抽象クラスという。

参考コード

抽象メソッドを定義するには、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;
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0