A practical example of Component inheritance and Override in Angular applications

I have an Angular Component Class, which I call A, which has a member model : O bservable < Product Search P age >, and then I create another Class B, and use B to extend A, in the code of B In it, I saw the overridemodel: Observable<ProductSearchPage>, and then I created another Class B, and used B to extend A. In the code of B, I saw the override model:Observable<ProductSearchPage>, Then I created another Class B and used B to ext e n d s A. In the code of B , I saw the usage of o v err i d e m o d e l : Observable = ….

In the above code, Ait is an Angular component class, which has a member model$of type Observable<ProductSearchPage>. Observableis a key concept in RxJS (a library for handling asynchronous operations and events), which represents a concept that can produce multiple values. In Angular, it is often used to handle asynchronous operations, such as HTTP requests.

Next we create a new class Band let Binherit from A. In TypeScript, a class can inherit the properties and methods of another class, which is called class inheritance. In this example, all properties and methods of Bare inherited , including .Amodel$

And then in the class Bwe see override model$: Observable<ProductSearchPage> = ...code like this. overrideThe keyword is a new feature added after TypeScript 4.3, which is used to override the properties or methods of the parent class in the subclass. In this example, the property in is overridden Bby . This means that when you access in an instance of class , you will access what is defined in class , not what is defined in class .override model$Amodel$Bmodel$Bmodel$Amodel$

Now, let's explain these concepts in detail.

  1. Class inheritance: In object-oriented programming, class inheritance is the ability that allows us to create new classes based on existing classes. The new class will inherit all the properties and methods of the existing class, and we can modify and extend the new class. In TypeScript, we use extendsthe keyword to implement class inheritance. For example, if we have a Personclass, we can create a Employeeclass that Employeeinherits from Personsuch that Employeewill have Personall of the properties and methods of .

  2. Property Override: In a subclass, we can override the properties and methods of the parent class. This means, we can provide a new property or method with the same name as the parent class in the subclass to replace the implementation of the parent class. In TypeScript 4.3 and later, we can use overridethe keyword to explicitly indicate that we want to override a property or method. If we try to override a non-existent property or method, TypeScript will give an error.

  3. Observable: In RxJS, Observableit is a key concept. A Observablecan emit multiple values, which can be emitted at any point in time. We can subscribe to one Observable, and when Observablethe value is emitted, our subscription code will execute. In Angular, Observableit is often used to handle asynchronous operations, such as HTTP requests.

In Angular, it is common practice to create a new class through inheritance, and this inheritance relationship can be applied to the Component class. When a class B inherits from class A, class B can override (override) members of class A, including properties, methods, and accessors. model$: Observable<ProductSearchPage>In the case you mentioned , class B overrides a member of class A.

Overriding members means redefining and implementing in derived classes (subclasses) members inherited from base classes (parent classes). This overriding allows the implementation details of inherited members to be modified in derived classes to meet specific needs.

The usage and meaning of override are explained below:

  1. Override attribute members:
    In class B, override model$: Observable<ProductSearchPage> = ...we redefine and override model$the attributes of class A through the syntax. In this case, model$the member has a new implementation in class B.

The sample code is as follows:

export class A {
    
    
  model$: Observable<ProductSearchPage>;
}

export class B extends A {
    
    
  override model$: Observable<ProductSearchPage> = new Observable<ProductSearchPage>();
}

In the above example, class B inherits from class A and the properties overrideare overridden by keywords model$. Here, we override the default implementation of the base class with a new Observable instance.

  1. Overriding method members:
    Similarly, overriding also applies to method members. In class B, methods in class A can be redefined and implemented, and overrideoverrides are identified by keywords.

The sample code is as follows:

export class A {
    
    
  method() {
    
    
    // 基类的实现
  }
}

export class B extends A {
    
    
  override method() {
    
    
    // 派生类B中的实现
  }
}

In the above example, class B inherits from class A and overrides method()the method of the base class. By reimplementing the method in class B, we can modify its behavior as needed.

  1. Overriding accessor members:
    Class B can also override accessor members (getters and setters) inherited from class A. By redefining and implementing accessors, we can customize the access and assignment behavior of properties in derived classes.

The sample code is as follows:

export class A {
    
    
  private _value: string;

  get value(): string {
    
    
    return this._value;
  }

  set value(newValue: string) {
    
    
    this._value = newValue;
  }
}

export class B extends A {
    
    
  private _value: string;

  override get value(): string {
    
    
    return 'Override: ' + this._value;
  }

  override set value(newValue: string) {
    
    
    this._value = 'Modified: ' + newValue;
  }
}

In the above example, class B overrides valuethe accessors of class A. By adding additional logic, we modify the behavior of accessing and setting property values ​​in derived classes.

Summarize

In Angular, class inheritance allows derived classes (subclasses) to override members of base classes (parent classes). The syntax for overrides uses overridekeywords and can be applied to members such as properties, methods, and accessors. By overriding, derived classes can modify the implementation details of inherited members to meet specific needs. This feature provides us with flexibility and extensibility, making it possible to customize and adjust the inherited behavior in derived classes as needed.

Guess you like

Origin blog.csdn.net/i042416/article/details/131649594