Abstract class (abstract class)
Trong các ngôn ngữ như Java hay PHP, có thể định nghĩa abstract class bằng modifier abstract. Abstract class là class không thể tạo instance trực tiếp. JavaScript không có cú pháp để định nghĩa abstract class. Ngược lại, TypeScript có modifier abstract để biểu diễn abstract class.
abstract được khai báo khi tạo abstract class. Abstract class là class không thể được khởi tạo trực tiếp (new), và đảm bảo chỉ được sử dụng như superclass. Method trong abstract class cũng có thể khai báo abstract. Tương tự như interface, subclass phải implement abstract method.
Khi thay đổi class Food thành abstract class và thêm method "cần bảo quản lạnh" keepRefrigerated() như một abstract method, class Meat sẽ báo lỗi. Lý do là class Meat chưa implement method keepRefrigerated.
tsabstract classFood {constructor(protectedname : string, protectedcalorie : number) {}showDebug () {console .log (`name = ${this.name } `);console .log (`calorie = ${this.calorie }kcal `);}abstractkeepRefrigerated (): boolean;}classNon-abstract class 'Meat' does not implement inherited abstract member keepRefrigerated from class 'Food'.2515Non-abstract class 'Meat' does not implement inherited abstract member keepRefrigerated from class 'Food'.extends Meat Food {}
tsabstract classFood {constructor(protectedname : string, protectedcalorie : number) {}showDebug () {console .log (`name = ${this.name } `);console .log (`calorie = ${this.calorie }kcal `);}abstractkeepRefrigerated (): boolean;}classNon-abstract class 'Meat' does not implement inherited abstract member keepRefrigerated from class 'Food'.2515Non-abstract class 'Meat' does not implement inherited abstract member keepRefrigerated from class 'Food'.extends Meat Food {}
Implement method keepRefrigerated() để xóa lỗi.
tsclassMeat extendsFood {keepRefrigerated (): boolean {return true;}}
tsclassMeat extendsFood {keepRefrigerated (): boolean {return true;}}
Điều gì xảy ra khi compile sang JavaScript
Abstract class của TypeScript vẫn tồn tại sau khi compile sang JavaScript, không bị xóa. Khi định nghĩa một abstract class rỗng và compile thì kết quả sẽ như thế nào?
tsabstract classAbstractClass {}
tsabstract classAbstractClass {}
Compile TypeScript trên sẽ sinh ra JavaScript sau:
Kết quả compiletsclass AbstractClass {}
Kết quả compiletsclass AbstractClass {}
Như vậy, abstract class được compile thành class thông thường với modifier abstract bị loại bỏ.
Abstract method bị xóa khi compile. Ví dụ, concreteMethod có implementation sẽ được giữ lại, nhưng abstract method abstractMethod sẽ bị xóa.
tsabstract classAbstractClass {concreteMethod (): void {/* Nội dung implementation */}abstractabstractMethod (): void;}
tsabstract classAbstractClass {concreteMethod (): void {/* Nội dung implementation */}abstractabstractMethod (): void;}
Kết quả compile TypeScript trên như sau:
Kết quả compiletsclass AbstractClass {concreteMethod() {/* Nội dung implementation */}}
Kết quả compiletsclass AbstractClass {concreteMethod() {/* Nội dung implementation */}}