TS: Extension of ES6 category
1. Public, private, protected modifiers
-
In TypeScript, the members of ES6 standard classes have been extended, that is,
public,private,protected,readonly
members can be modified to limit their use domains. -
Understand the difference between protected and private:
-
The protected member can still be accessed in its derived class (subclass);
-
If the constructor is marked as protected, it means that this class can no longer be instantiated outside of its class, but can be inherited:
class Person { protected name : string; protected constructor(theName : string){ this.name = theName; } } // Employee 能够继承 Person class Employee extends Person { private department: string; constructor(name: string, department: string) { super(name); // 可以实现 this.department = department; } public getElevatorPitch() { return `Hello, my name is ${ this.name} and I work in ${ this.department}.`; } } let howard = new Employee("Howard", "Sales"); // ok,Employee类继承了Person,拥有自己的构造方法 let john = new Person("John"); // 错误: 'Person' 的构造函数是被保护的.
-
2. Parameter attributes
-
In the above example, we define a name and a constructor whose parameter is theName in the class, and assign theName to name. Parameter attributes can combine the two;
-
Parameter attributes are declared by adding an access qualifier in front of the constructor parameters.
class Person { constructor(readonly name : string) { }; }
3. Accessor
-
TypeScript supports the use of getters and setters to intercept access to object members.
-
Look at an example of setting a password:
class ID { private _password : number; get password() : number{ return this._password; } set password(oldPwd: number ,newPwd : number){ if(oldPwd && oldPwd === _password){ this._password = newPwd; }else{ console.log("Error : old password is not valid"); } } }
4. Static Properties
-
Like Java, TypeScript also supports direct access
类名.属性
to static properties.class Person { static proto : string = 'human'; } console.log(Person.proto); // 'human'
5. Abstract class
-
Abstract classes are used as base classes for other derived classes. They are generally not instantiated directly;
-
Like Java, the abstract methods of abstract classes do not need to provide specific implementations, but they must be implemented in derived classes;
abstract class Person { constructor(public name : string){ }; printName() : void { console.log('my name is ' + this.name); } abstract printMeeting() : void; // 必须在子类中实现 } class Employee extends Person{ constructor(){ super('yivi'); } printMeeting(): void{ console.log('I am Employee ' + this.name); } printReports(): void{ console.log('I am not exist'); } } let p : Person; // ok,允许创建一个对抽象类型的引用 p = new Person(); // error,不允许创建一个抽象类的实例’ p = new Employee(); // ok p.printName(); p.printMeeting(); p.printReports(); //error,方法在生命的抽象类中不存在。
6. Use the class as an interface
-
Because classes can create types with i, we can use classes where interfaces are allowed.
class Point { x : number; y : number; } interface Point3D extends Point { z : number; } let point : Point3D = { x : 1, y : 2, z : 3 }