如何在 TypeScript 中创建抽象类?


抽象简介

我们希望读者在实现抽象类之前,能够熟悉抽象类及其要求。抽象意味着隐藏。它用于向用户和某些开发人员隐藏底层代码实现。此外,它用于仅显示方法的必要信息,而不是显示方法的整个复杂实现。

创建抽象类

我们可以使用 abstract 关键字来定义抽象类或方法。抽象类可以包含普通和抽象类型的方法。在抽象类中,我们需要实现功能或普通方法,并且只需要声明抽象方法。

我们可以使用任何其他类继承抽象类,但我们需要将抽象类中的所有抽象方法实现到继承类中。如果我们不想在继承类中实现抽象方法,我们需要使用 abstract 关键字将继承类也设为抽象类。

此外,我们不能创建抽象类的对象,但可以创建继承类的对象并使用抽象类方法。抽象类的限制是,我们不能使用多个抽象类实现多重继承。

语法

用户可以按照以下语法创建抽象类并将其继承到其他类。

abstract class sample {
   // define variables inside the abstract class,
   // declare the abstract methods or non-abstract method inside the abstract class
   abstract demo(string): void;
}
// extend sample class and implement all abstract methods of sample to demo class
class test extends sample {
   demo(name: string): void {
      // code for the demo method
   }
}

步骤

  • 步骤 1 − 定义名为 sample 的抽象类,其中包含名为 propert1 和 property2 的属性。

  • 步骤 2 − 此外,在 sample 类中添加构造函数以初始化 property1 和 property2 的值。

  • 步骤 3 − 在抽象类内部声明抽象方法名称 demo()。此外,定义抽象类的 save() 方法,这是一个非抽象方法,用于打印 property1 和 property2 的值。

  • 步骤 4 − 定义 test 类,这是一个非抽象类,继承自 sample 类。此外,将字符串类型的 property3 添加到 test 类。

  • 步骤 5 − 向 test 类添加构造函数,该构造函数接受 3 个参数,并使用前 2 个参数调用超类的构造函数,即 sample 类。

  • 步骤 6 − 在 test 类内部实现 sample 类的 demo() 方法,该方法打印 property3 的值。

示例 1

在下面的示例中,我们定义了包含抽象方法的抽象类。在继承的 test 类中,我们实现了 sample 类的抽象方法。接下来,我们使用 3 个参数创建了 test 类的对象,并使用该对象调用了 demo() 和 save() 方法。

abstract class sample {
   // define variables inside the abstract class,
   property1: string;
   constructor(property1: string, property2: number) {
      this.property1 = property1;
   }
   // declare the abstract methods
   abstract demo(): void;
   
   // defining the non-abstract methods
   save(): void {
      console.log("The save method of the abstract class is executed.");
   }
}
// extend sample class and implement all abstract methods of sample to demo class
class test extends sample {
   property2: number;
   constructor(property1: string, property2: number) {
      super(property1);
      this.property2 = property2;
   }
   demo(): void {
      // code for the demo method
      console.log("The value of the property 3 is " + this.propert2);
   }
}
let test_obj = new test("TutorialsPont", 9999);
test_obj.demo();
test_obj.save();

在上面的示例中,我们隐藏了继承类 test 中 save() 方法的实现。我们允许开发人员根据需要实现 demo() 方法,但隐藏其他类信息,例如 property1、property2 和 save() 方法的实现。

现在,用户正确理解了使用抽象类的动机以及我们如何使用它来隐藏信息并仅显示所需信息。

编译后,以上代码将生成以下 JavaScript 代码:

var __extends = (this && this.__extends) || (function () {
   var extendStatics = function (d, b) {
      extendStatics = Object.setPrototypeOf ||
      ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
      function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
      return extendStatics(d, b);
   };
   return function (d, b) {
      extendStatics(d, b);
      function __() { this.constructor = d; 
   }
   d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var sample = /** @class */ (function () {
   function sample(property1, property2) {
      this.property1 = property1;
   }
   // defining the non-abstract methods
   sample.prototype.save = function () {
      console.log("The save method of the abstract class is executed.");
   };
   return sample;
}());
// extend sample class and implement all abstract methods of sample to demo class
var test = /** @class */ (function (_super) {
   __extends(test, _super);
   function test(property1, property2) {
      var _this = _super.call(this, property1) || this;
      _this.property2 = property2;
      return _this;
   }
   test.prototype.demo = function () {
      // code for the demo method
      console.log("The value of the property 3 is " + this.propert2);
   };
   return test;
}(sample));
var test_obj = new test("TutorialsPont", 9999);
test_obj.demo();
test_obj.save();

输出

它将产生以下输出:

The value of the property 3 is undefined
The save method of the abstract class is executed.

示例 2

在下面的示例中,class1 是抽象类,其中包含抽象方法名称 method1 的声明。class2 仅包含 method2() 的定义。它扩展了 class1 但没有实现名为 method1() 的抽象方法。

之后,我们定义了 class3 并通过 class2 继承它。此外,我们在 class3 中定义了 class 内部 method1。最后,我们创建了 class3 的对象并调用了 method1() 和 method2()。

// define the abstract class1 containing the abstract method1
abstract class class1 {
   abstract method1(): void;
}
// Need to create class2 to abstract as we inherited class1 but doesn't defined abstract method1()
abstract class class2 extends class1 {
   method2(): void {
      console.log("Inside the method 2 of class2.");
   }
}
// defining the class3 inherited by the class2
class class3 extends class2 {
   // Implementation of the method1 of the abstract class1
   method1(): void {
      console.log(
         "Implemented the abstract method name method1 of class1 inside the class3"
      );
   }
}
// Crating the object of the class3
var object = new class3();

// Invoking the method1 of class1 which is declared in the abstract class1
object.method1();

// Invoking the method2 of class2
object.method2();

上面的示例向我们展示了,如果我们通过任何类继承抽象类并且不想在继承类中实现抽象方法,则需要将继承类设为抽象类。

编译后,以上代码将生成以下 JavaScript 代码:

var __extends = (this && this.__extends) || (function () {
   var extendStatics = function (d, b) {
      extendStatics = Object.setPrototypeOf ||
         ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
         function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
         return extendStatics(d, b);
      };
      return function (d, b) {
         extendStatics(d, b);
         function __() { this.constructor = d; }
         d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
      };
})();
// define the abstract class1 containing the abstract method1
var class1 = /** @class */ (function () {
   function class1() {
   
   }
   return class1;
}());
// Need to create class2 to abstract as we inherited class1 but doesn't defined abstract method1()
var class2 = /** @class */ (function (_super) {
   __extends(class2, _super);
   function class2() {
      return _super !== null && _super.apply(this, arguments) || this;
   }
   class2.prototype.method2 = function () {
      console.log("Inside the method 2 of class2.");
   };
   return class2;
}(class1));
// defining the class3 inherited by the class2
var class3 = /** @class */ (function (_super) {
   __extends(class3, _super);
   function class3() {
   return _super !== null && _super.apply(this, arguments) || this;
   }
   // Implementation of the method1 of the abstract class1
   class3.prototype.method1 = function () {
      console.log("Implemented the abstract method name method1 of class1 inside the class3");
   };
   return class3;
}(class2));
// Crating the object of the class3
var object = new class3();

// Invoking the method1 of class1 which is declared in the abstract class1
object.method1();

// Invoking the method2 of class2
object.method2();

输出

它将产生以下输出:

Implemented the abstract method name method1 of class1 inside the class3
Inside the method 2 of class2.

用户在本教程中学习了如何实现抽象类。现在,用户可以理解我们如何使用抽象类和抽象方法隐藏类的其他信息。

此外,用户可以使用接口进行抽象。TypeScript 中接口的所有方法都是抽象的,并且不允许非抽象方法。此外,我们可以使用接口进行多重继承。

更新于: 2023年1月16日

1K+ 浏览量

启动你的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.