类使用关键字 class
定义。
class Greeter {
constructor(message: string) {
this.greeting = message;
}
greeting: string;
greet() {
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("world");
通过 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); // 构造中需要调用super
}
move(distanceInMeters = 5) {
console.log("Slithering...");
super.move(distanceInMeters);
}
}
class Horse extends Animal {
constructor(name: string) {
super(name);
}
move(distanceInMeters = 45) {
console.log("Galloping...");
super.move(distanceInMeters);
}
}
let sam = new Snake("Sammy the Python");
let tom: Animal = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
- 公共(Public) : 默认缺省修饰符
- 私有(Private)
- 受保护的修饰符(Protected)
class Animal {
public name: string;
}
// 等价
class Animal {
name: string;
}
私有属性或方法只能在类内部访问。
class Animal {
private name: string;
constructor(theName: string) { this.name = theName; }
getName(){
return this.name;
}
}
new Animal("Cat").name; // 错误: 'name' 是私有的.
new Animal("Catie").getName(); // 正确!Catie
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成员
}
}
let howard = new Employee("Howard", "Sales");
console.log(howard.getElevatorPitch());
console.log(howard.name); // 错误!不能在类外边访问
你可以使用 readonly关键字将属性设置为只读的。 只读属性必须在声明时或构造函数里被初始化。
class Octopus {
readonly name: string;
readonly numberOfLegs: number = 8;
constructor (theName: string) {
this.name = theName;
}
}
let dad = new Octopus("Man with the 8 strong legs");
dad.name = "Man with the 3-piece suit"; // 错误! name 是只读的.
在构造参数上限定访问修饰符
class Animal {
constructor(private name: string) { } // 定义private修饰符
move(distanceInMeters: number) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
参数属性通过给
构造函数
参数添加一个访问限定符
来声明。
使用 private限定一个参数属性会 声明 并 初始化 一个 私有成员 ;对于 public和 protected来说也是一样。
let passcode = "secret passcode";
class Employee {
private _fullName: string; // 限定private
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) {
alert(employee.fullName);
}
静态成员,这些属性存在于类本身上面而不是类的实例上。
使用 static
定义静态属性,静态属性通过 Class.prop
方式直接访问。
class Grid {
static origin = {x: 0, y: 0};
calculateDistanceFromOrigin(point: {x: number; y: number;}) {
let xDist = (point.x - Grid.origin.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 class Animal { // 定义抽象类
abstract makeSound(): void; // 定义抽象方法
move(): void { // 定义具体方法实现
console.log('roaming the earch...');
}
}
抽象类中可以有抽象方法和具体实现,不要和接口弄混了。