欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

typescript中的类(笔记)

程序员文章站 2022-07-03 20:53:30
...

1.类:

class Greeter {
	greeting: string;
	Constructor(message: string){
		This.greeting = mesage;
	}
	greet(){
		Return “hello”+this.greeting;
	}
}
Let greeter = new Greeter(“world”);

2.继承:

Class Animal {
	Move(distance: number = 0){
		Console.log(`animal moved ${distance}m.`);
	}
}
Class Dog extends Animal{
	Bark(){
		Console.log(‘woof!woof!);
	}
}
Const dog = new Dog();
dog.bark();
dog.move(10);
dog.bark();

3.公共,私有与受保护的修饰符
(1)public: 在ts中,成员都默认为public,也可以明确地将一个成员标记为public:

Class Animal {
Public name: string;
Public constructor(theName: string) { this.name = theName;}
Public move(distance: number){
	Console.log(`${this.name} moved ${distance}m.`);
}

(2)Private: 当成员被标记为private时,他就不能在声明它的类的外部访问;

Class Animal{
	Private name: string;
	Constructor(theName: string){
		This.name = theName;
	}
}
new Animal(“cat”).name;  //错误,name是私有的

(3)protected: 与private相似,不同的是,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}.`; 
	}
} 
Let	howard	= new Employee("Howard", "Sales"); 	
console.log(howard.getElevatorPitch()); 
console.log(howard.name); // 错误

注意,我们不能在 Person类外使用 name,但是我们仍然可以通过 Employee类的实例方法访问,因为 Employee是由 Person派生而来的。
构造函数也可以被标记成 protected。 这意味着这个类不能在包含它的类外被实例化,但是能被继承.

class Person { 
	protected name: string; 
	protected constructor(theName: string) { 
		this.name = theName; 
	}
 } 
let john = new Person("John"); // 错误: 'Person' 的构造函数是被保护的.

你可以使用 readonly关键字将属性设置为只读的。 只读属性必须在声明时或构造函数里被初始化。

C lass Optopus{
	Readonly numberFlag: number = 8;
	Constructor(readonly name: string){...}
}
  1. 存取器:TypeScript支持通过getters/setters来截取对对象成员的访问。 它能帮助你有效的控制对对象成员的访问。
let passcode = "secret passcode";
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) { 
	alert(employee.fullName); 
}
  1. 静态属性: 不能通过实例对象调用,可以通过类名直接调用;
  2. 抽象类: 不能被实例化,抽象类中的抽象方法不能有方法体并且必须在子类中实现,抽象方法必须包括abstract关键字并且可以包含访问修饰符
abstract class Department { 
	constructor(public name: string) { } 
	printName(): void { 
		console.log('Department name: ' + this.name); 
	} 
	abstract printMeeting(): void; // 必须在派生类中实现 
} 
class AccountingDepartment extends Department { 
	constructor() { super('Accounting and Auditing');
 		// 在派生类的构造函数中必须调用 
		super() 
	} 
	printMeeting(): void { 
		console.log('The Accounting Department meets each Monday at 10am.'); 
	} 
	generateReports(): void { 
		console.log('Generating accounting reports...'); 
	} 
} 
let department: Department; // 允许创建一个对抽象类型的引用 	department = new Department(); // 错误: 不能创建一个抽象类的实例 
department = new AccountingDepartment(); // 允许对一个抽象子类进行实例化和赋值
department.printName(); 
department.printMeeting(); 
department.generateReports(); // 错误: 方法在声明的抽象类中不存在
  1. 把类当作接口
class Point { x: number; y: number; } 
interface Point3d extends Point {
 	z: number;
 } 
let point3d: Point3d = {x: 1, y: 2, z: 3};