TypeScript class

Basic use of class

class Person {
  _name: string;
  age: number;

  constructor(p: { _name: string; age: number }) {
    this._name = p._name;
    this.age = p.age;
    console.log(b);
  }

  eating() {
    console.log(this.age, this._name); }}const xm = new Person({ _name: 'Ming'.age: 24 });
xm.eating();
Copy the code

The class inheritance

class Person {
  _name: string;
  age: number = 0;

  constructor(_name: string, age? :number) {
    this._name = _name;
    age && (this.age = age);
  }

  eating() {
    console.log('eating'); }}class Student extends Person {
  sno: number = 0;

  constructor(_name: string, age: number, sno? :number) {
    super(_name, age); // Parent class constructor
    sno && (this.sno = sno);
  }

  eating() {
    // You can override the parent method if you are not satisfied with it
    super.eating(); // Call the parent original method
    console.log('Subclass new method');
  }

  studying() {
    console.log('studying'); }}const xm = new Student('Ming'.23);
Copy the code

The class modifier

  • Public the default is public, open

  • Private Specifies that the property can only be accessed internally by the class.

  • Protected Defines that this property can only be accessed from this class or a subclass of that class

  • Readonly Defines that the property is read-only

    class Person {
      private _name: string = ' '; // Define private attributes (not accessible outside the class)
      protected protectedKey: string =
        'this is a protected attribute, can only in the class | subclasses of internal access';
    
      readonlyfriend? : Person;constructor(_name: string, friend? : Person) {
        this._name = _name;
        this.friend = friend; }}class Student extends Person {
      getProtected() {
        console.log(this.protectedKey); }}new Person('ccc'.new Person('zn'));
    Copy the code

Class accessors get/set

class Person {
  private _name: string = ' ';

  get name() {
    return this._name;
  }

  set name(newName: string) {
    this._name = newName; }}new Person().name = 'cqc'; // set
Copy the code

An abstract class

Features:

  1. Abstract classes cannot be instantiated
  2. Abstract properties must be in the abstract class (abstract methods do not need a method body, and abstract properties do not need a value)
  3. Other classes that inherit from this abstract class must implement the abstract properties of that abstract class, == unless the inherited class is still an abstract class
  • Application scenarios When defining a lot of == generic == interfaces, we usually let the caller pass in the parent class to achieve a more flexible way of calling by abortion. In the parent class, we do not need to implement certain methods (in some public methods to ensure that the parameters passed in are correct).
// The abstract class cannot be instantiated
abstract class Shape {
  /* abstract indicates that the abstract property must be written in the abstract class. If it is an abstract method, it does not need the method body */
  abstract getArea(): number; // You need to implement it yourself
  name = 'I'm an abstract class'; // It is inherited normally
}

class Circle extends Shape {
  getArea() {} // Implement abstract attributes in abstract class
}

class Rectangle extends Shape{
  getArea() {} // Implement abstract attributes in abstract class
}

// Ensure that the attribute passed in has the required method
function makeArea(shape: Shape) {...return shape.getArea();
}
Copy the code