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, A
it is an Angular component class, which has a member model$
of type Observable<ProductSearchPage>
. Observable
is 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 B
and let B
inherit 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 B
are inherited , including .A
model$
And then in the class B
we see override model$: Observable<ProductSearchPage> = ...
code like this. override
The 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 B
by . 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$
A
model$
B
model$
B
model$
A
model$
Now, let's explain these concepts in detail.
-
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
extends
the keyword to implement class inheritance. For example, if we have aPerson
class, we can create aEmployee
class thatEmployee
inherits fromPerson
such thatEmployee
will havePerson
all of the properties and methods of . -
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
override
the 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. -
Observable: In RxJS,
Observable
it is a key concept. AObservable
can emit multiple values, which can be emitted at any point in time. We can subscribe to oneObservable
, and whenObservable
the value is emitted, our subscription code will execute. In Angular,Observable
it 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:
- Override attribute members:
In class B,override model$: Observable<ProductSearchPage> = ...
we redefine and overridemodel$
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 override
are overridden by keywords model$
. Here, we override the default implementation of the base class with a new Observable instance.
- Overriding method members:
Similarly, overriding also applies to method members. In class B, methods in class A can be redefined and implemented, andoverride
overrides 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.
- 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 value
the 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 override
keywords 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.