[TypeScript] Use of abstract classes | Types of classes

Supplement to the basic usage of TypeScript classes

Use of abstract classes

We know that inheritance is a prerequisite for the use of polymorphism .

Therefore, when defining many common calling interfaces, we usually let the caller pass in the parent class to achieve a more flexible calling method through polymorphism.

However, the parent class itself may not need to implement specific methods , so the methods defined in the parent class can be defined as abstract methods .

What is an abstract method? A method that has no concrete implementation (no method body) in TypeScript is an abstract method .

An abstract class is a class declared using abstract;

The abstract method is a method declared using abstract, the abstract method must exist in the abstract class, and the abstract method has no method body;

Let's look at the following example

For example, in the following code, we have a function makeArea, which is used to calculate the area of ​​a graph. This function can calculate the area of ​​graphs such as circles and rectangles, but in the function, which graph area is realized? Which graph is realized? The area is not suitable, so we can pass in a parameter, and all the passed parameters are required to implement the getArea method, we only need to call getArea in the function

Create another parent class, define the parent class as an abstract class, and define an abstract method getArea in the abstract class

When we need to calculate the area of ​​which graphic, we can create the class that requires the required area graphic, let it inherit from the parent class (abstract class), and implement the abstract method of the parent class. For example, in the following code, I am respectively Created the class of rectangular area and circular area, let them inherit from the parent class Shape

function makeArea(shape: Shape) {
    
    
  return shape.getArea()
}

// 定义抽象父类
abstract class Shape {
    
    
  // 定义抽象方法
  abstract getArea(): number
}

// 1.定义计算矩形面积的类
class Rectangle extends Shape {
    
    
  width: number
  height: number

  constructor(width: number, height: number) {
    
    
    super()
    this.width = width
    this.height = height
  }

  getArea() {
    
    
    return this.width * this.height
  }
}

// 2.定义计算圆形面积的类
class Circle extends Shape {
    
    
  r: number

  constructor(r: number) {
    
    
    super()
    this.r = r
  }

  getArea() {
    
    
    return this.r * this.r * 3.14
  }
}


// 测试
const reactangle = new Rectangle(10, 20)
const circle = new Circle(2)

console.log(makeArea(reactangle))
console.log(makeArea(circle))

Abstract classes have the following characteristics :

Abstract classes cannot be instantiated (that is, abstract classes cannot be created with new)

The abstract method must be implemented by the subclass , otherwise the class must be an abstract class, if it is an abstract class, it can not be implemented;


type of class

The class itself can also be used as a data type :

  • For example, in the following code, at this time p is of type Person
class Person {
    
    
  name: string = "aaa"

  eating() {
    
    
    console.log("eating")
  }
}

const p = new Person()
  • We can also create a variable p in a literal way, requiring it to be of type Person, then we need to ensure that the corresponding properties and methods need to be consistent with Person
class Person {
    
    
  name: string = "aaa"

  eating() {
    
    
    console.log("eating")
  }
}

const p: Person = {
    
    
  // 属性需要和Person类保持一致
  name: "chenyq",
  eating() {
    
    
    console.log("p eating")
  }
}

Application scenarios

For example, in development, our encapsulated function needs to be required to pass in the type of a class, so that restrictions can ensure that the properties or methods of the class can be used normally in the function.

  • We can pass in an instance object of the Person class
class Person {
    
    
  name: string
  constructor(name: string) {
    
    
    this.name = name
  }
}

function printName(p: Person) {
    
    
  console.log(p.name)
}

// 测试
const p1 = new Person("chenyq")
const p2 = new Person("kaisa")

printName(p1)
printName(p2)
  • You can also pass in objects that correspond to the attributes and methods of the Person class one-to-one.
class Person {
    
    
  name: string
  eating() {
    
    
    console.log("eating")
  }
  constructor(name: string) {
    
    
    this.name = name
  }
}

function printName(p: Person) {
    
    
  console.log(p.name)
}

// 测试
printName({
    
     name: "kaisa", eating() {
    
    } })

Guess you like

Origin blog.csdn.net/m0_71485750/article/details/126336606