From dc01cafc8061885e1ba5fc1db1d45598e8f0d2b0 Mon Sep 17 00:00:00 2001 From: ryotatake <39555429+ryotatake@users.noreply.github.com> Date: Wed, 4 Aug 2021 14:35:16 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B5=E3=83=B3=E3=83=97=E3=83=AB=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://basarat.gitbook.io/typescript/future-javascript/classes に記載されているサンプルコードが書かれていなかったので追加しました。 --- future-javascript/classes/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/future-javascript/classes/README.md b/future-javascript/classes/README.md index 4b903b136..909393b52 100644 --- a/future-javascript/classes/README.md +++ b/future-javascript/classes/README.md @@ -131,8 +131,37 @@ class FooChild extends FooBase { `abstract`はアクセス修飾子の1つと考えることができます。上で述べた修飾子とは異なり、クラスのメンバと同様に`class`に対しても利用できるため、アクセス修飾子とは分けて説明します。`abstract`修飾子が主に意味することは、その機能を親クラスに対して直接的に呼び出すことができず、子クラスがその具体的な機能を提供しなければならないということです。 * 抽象クラスを直接インスタンス化することはできません。その代わりに、`abstract class`を継承した`class`を作成しなければなりません + +```typescript +abstract class FooCommand {} + +class BarCommand extends FooCommand {} + +const fooCommand: FooCommand = new FooCommand(); // Cannot create an instance of an abstract class. + +const barCommand = new BarCommand(); // You can create an instance of a class that inherits from an abstract class. +``` + * 抽象メンバは直接アクセスできません。子クラスがその具体的な機能(実装)を提供しなくてはなりません +```typescript +abstract class FooCommand { + abstract execute(): string; +} + +class BarErrorCommand extends FooCommand {} // 'BarErrorCommand' needs implement abstract member 'execute'. + +class BarCommand extends FooCommand { + execute() { + return `Command Bar executed`; + } +} + +const barCommand = new BarCommand(); + +barCommand.execute(); // Command Bar executed +``` + ## コンストラクタは必須ではありません クラスは必ずコンストラクタを持っている必要はありません。例えば、以下は正しく動作します。