This is the 21st day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
If you feel helpful, please click 👍 to encourage you
Abstract Class
- Classes that begin with abstract are called abstract classes
- Abstract classes are not much different from other classes except that they cannot be used to create objects
- Abstract classes are classes that are designed to be inherited by other classes. They can only be inherited by other classes and cannot be used to create instances
- Abstract methods can be added to an abstract class
- Methods that start with abstract are called abstract methods. Abstract methods have no body and can only be defined in abstract classes. When inheriting abstract classes, the abstract methods must be overridden or an error will be reported
abstract class Animal {
name: String;
constructor(name: string) {
this.name = name;
}
abstract bark(): void;
}
class Dog extends Animal {
bark() {
console.log('Want want Want'); }}Copy the code
Attribute modifier
By default, the properties of an object can be modified at will. To ensure data security, the permission of the properties can be set in TS
TS attributes have three modifiers:
- Public (default), which can be modified in classes, subclasses, and objects
- Protected, which can be modified in classes and subclasses
- Private, which can be changed in the class
public
class Person{
public name: string; // Write or write nothing is public
public age: number;
constructor(name: string, age: number){
this.name = name; // Can be changed in the class
this.age = age;
}
sayHello(){
console.log('Hello, I amThe ${this.name}`); }}class Employee extends Person{
constructor(name: string, age: number){
super(name, age);
this.name = name; // Subclasses can be modified}}const p = new Person(Sun Wukong.18);
p.name = 'Pig Eight Quit';// Can be modified by object
Copy the code
protected
class Person{
protected name: string;
protected age: number;
constructor(name: string, age: number){
this.name = name; // It can be modified
this.age = age;
}
sayHello(){
console.log('Hello, I amThe ${this.name}`); }}class Employee extends Person{
constructor(name: string, age: number){
super(name, age);
this.name = name; // Subclasses can be modified}}const p = new Person(Sun Wukong.18);
p.name = 'Pig Eight Quit';// Cannot be modified
//Property 'name' is protected and only accessible within class 'Person' and its subclasses.
Copy the code
private
class Person{
private name: string;
private age: number;
constructor(name: string, age: number){
this.name = name; // It can be modified
this.age = age;
}
sayHello(){
console.log('Hello, I amThe ${this.name}`); }}class Employee extends Person{
constructor(name: string, age: number){
super(name, age);
this.name = name; // Cannot be modified in subclasses
//Property 'name' is private and only accessible within class 'Person'.}}const p = new Person(Sun Wukong.18);
p.name = 'Pig Eight Quit';// Cannot be modified
//Property 'name' is private and only accessible within class 'Person'.
Copy the code
Attribute accessor
- For properties that do not want to be arbitrarily modified, you can set them to private. Setting them to private directly will cause them to be unable to be modified through the object
- We can define a set of methods in a class that read and set properties, called accessors for properties
- The methods that read properties are called setter methods, and the methods that set properties are called getter methods
class Person{
private _name: string;
constructor(name: string){
this._name = name;
}
/ / get the property name
get name() {return this._name;
}
/ / set the attribute name
set name(name: string) {this._name = name; }}const p1 = new Person(Sun Wukong);
// The name property is actually read by calling the getter method
console.log(p1.name);
// Actually modify the name property by calling setter methods
p1.name = 'Pig Eight Quit';
Copy the code
shorthand
- Adding modifiers to parameters serves the purpose of shorthand
- Do not declare name:string
- The constructor does not use this.name=name
class Person{
constructor(public name: string){}}/ / equivalent to the
class Person {
name: string;
constructor(name: string) {
this.name = name; }}Copy the code