跟绝大数面对对象语言一样,TypeScript中的类也支持定义构造方法、实例属性、实例方法。
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("world");
使用关键字new
创建一个实例。
TypeScript支持Class继承,使用关键字extends
。
class Animal {
name: string;
constructor(theName: string) { this.name = theName; }
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Snake extends Animal {
constructor(name: string) { super(name); }
move(distanceInMeters = 5) {
console.log("Slithering...");
super.move(distanceInMeters);
}
}
子类若包含构造函数,则必须调用
super()
来执行基类的构造函数。
TypeScript中类成员访问修饰符默认是public
,即类外部可以自由地访问类中成员。
class Animal {
public name: string;
public constructor(theName: string) { this.name = theName; }
public move(distanceInMeters: number) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
使用private
修饰的类成员不能被类外部代码访问。
class Animal {
private name: string;
constructor(theName: string) { this.name = theName; }
}
使用protected
修饰的类成员能被子类中的代码访问。
class Person {
protected name: string;
constructor(name: string) { this.name = name; }
}
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}.`;
}
}
若使用
protected
修饰构造函数,则该类不允许在类外部实例化,但是可以被继承。
使用关键字readonly
修饰的属性只能在属性声明时或构造函数中对它赋值。
class Octopus {
readonly name: string;
readonly numberOfLegs: number = 8;
constructor (theName: string) {
this.name = theName;
}
}
当构造函数的参数使用访问修饰符或readonly
修饰符修饰时,编译器自动定义一个相同名称的属性并给它赋值。
class Octopus {
readonly numberOfLegs: number = 8;
constructor(readonly name: string) {
}
}
使用关键字get
和set
定义属性访问器。
class Employee {
private _fullName: string;
get fullName(): string {
return this._fullName;
}
set fullName(newName: string) {
if (passcode && passcode == "secret passcode") {
this._fullName = newName;
}
else {
console.log("Error: Unauthorized update of employee!");
}
}
}
let employee = new Employee();
employee.fullName = "Bob Smith";
if (employee.fullName) {
console.log(employee.fullName);
}
使用关键字static
声明静态属性。
class Grid {
static origin = {x: 0, y: 0};
calculateDistanceFromOrigin(point: {x: number; y: number;}) {
let xDist = (point.x - Grid.origin.x);
let yDist = (point.y - Grid.origin.y);
return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale;
}
constructor (public scale: number) { }
}
使用关键字abstract
定义抽象类,抽象类不能被实例化。在抽象类中,可以使用abstract
定义抽象方法。
abstract class Animal {
abstract makeSound(): void;
move(): void {
console.log("roaming the earth...");
}
}
抽象方法必须在子类中实现。
使用关键字implements
约束类必须实现指定的接口。
interface ClockInterface {
currentTime: Date;
}
class Clock implements ClockInterface {
currentTime: Date;
constructor(h: number, m: number) { }
}