インターフェースを実装する
TypeScriptでは、クラスがインターフェースを実装できます。実装するにはimplementsキーワードを用います。
tsHuman {think (): void;}classDeveloper implementsHuman {think (): void {console .log ("どういう実装にしようかな〜");}}
tsHuman {think (): void;}classDeveloper implementsHuman {think (): void {console .log ("どういう実装にしようかな〜");}}
インターフェースを複数指定することもできます。そのときは,でインターフェースを区切り列挙します。
tsHuman {think (): void;}interfaceProgrammer {writeCode (): void;}classTypeScriptProgrammer implementsHuman ,Programmer {think (): void {console .log ("どういうコードにしようかな〜");}writeCode (): void {console .log ("カタカタ");}}
tsHuman {think (): void;}interfaceProgrammer {writeCode (): void;}classTypeScriptProgrammer implementsHuman ,Programmer {think (): void {console .log ("どういうコードにしようかな〜");}writeCode (): void {console .log ("カタカタ");}}
インターフェースで定義されたフィールドをクラスで実装するには、クラス側にはフィールドを定義します。
tsHuman {name : string;}classDeveloper implementsHuman {name : string = "Bob";}
tsHuman {name : string;}classDeveloper implementsHuman {name : string = "Bob";}