TypeScript インターフェース、クラス
TypeScripの基本的な使い方をやります。インターフェース
interface TestInterface {
msgA: string;
msgB: string;
print(): void;
}
const testI: TestInterface = {
msgA: "Hello",
msgB: "World",
print(){
console.log(testI.msgA + testI.msgB)
},
};
testI.print();
testI.msgB = "World2";
testI.print();
クラス
class TestClass {
msgA: string;
msgB: string;
constructor(a: string, b: string) {
this.msgA = a;
this.msgB = b;
}
print(): void {
console.log(this.msgA + this.msgB);
};
}
const testC: TestClass = new TestClass("Hello", "World");
testC.print();
testC.msgB = "World2";
testC.print();
実装
interface TestInterface {
msgA: string;
msgB: string;
print(): void;
}
class TestClass implements TestInterface {
msgA: string;
msgB: string;
constructor(a: string, b: string) {
this.msgA = a;
this.msgB = b;
}
print(): void {
console.log(this.msgA + this.msgB);
};
}
const testC: TestInterface = new TestClass("Hello", "World");
testC.print();
testC.msgB = "World2";
testC.print();
継承
class TestClass {
msgA: string;
msgB: string;
constructor(a: string, b: string) {
this.msgA = a;
this.msgB = b;
}
print(): void {
console.log(this.msgA + this.msgB);
};
}
class TestClass2 extends TestClass{
print(): void {
super.print();
console.log(this.msgA + " --- " + this.msgB);
};
}
const testC: TestClass = new TestClass2("Hello", "World");
testC.print();
testC.msgB = "World2";
testC.print();
ページのトップへ戻る