class Greeter{
greeting:string;
constructor(message: string){
this.greeting = message;
}
greet(){
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("world");
繼承在ts裏,我們可以使用常用的面向對象模式。
基於類的設計模式中一種最基本的模式是允許使用繼承來擴展現有的類。
class Animal{
move(distanceInMeters: number = 0){
console.log('Animal moved ${distanceInMeters}m.');
}
}
class Dog extends Animal{
bark(){
console.log('Woof! Woof!');
}
}
const dog = new Dog();
dog.bark();
dog.move(10);
========================================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 : number = 0){
console.log('${this.name} moved ${distanceInMeters}m');
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
protected子類可以訪問
class Animal{
constructor(private name: string){
}
move(distanceInMeters : number){
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
readonly修飾符你可以使用readonly關鍵字將屬性設置爲只讀的。只讀屬性必須在聲明時或者構造函數裏被初始化
參數屬性
在上面的例子中,我們不得不定義一個受保護的成員name和一個構造函數參數theName在Person類裏,並且立刻將theName的值付給name。
class Animal{
constructor(private name : string){
}
move(distanceInMeters: number){
//......
}
}
存取器class Employee{
private _fullName : string;
get fullName():string{
return this._fullName;
}
set fullName(newName : string){
if(password == "password"){
this._fullName = newName;
}else{
console.log("Error: Unauthorized update of employee!");
}
}
}
靜態屬性class Gird{
static origin = {x : 0,y: 0};
calculateDistanceFromOrgin(point:{x:number;y:number;}){
let xDist = (point.x - Gird.origin.x);
let yDist = (point.y - Gird.origin.y);
return Math.sqrt(xDist * xDist + yDist * yDist)/this.scale;
}
constructor(public scale: number){
}
}
let grid1 = new Grid(1.0);
let gri = new Grid(5.0);
console.log(grid1.calculateDistanceFromOrigin({x: 10, y: 10}));
抽象類抽象類做爲其它派生類的基類使用。他們一般不會直接被實例化。
不同於接口,抽象類可以包含成員的實現細節。
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();//error
department = new AccountingDepartment();
高級技巧構造函數
當你在TypeScript裏聲明瞭一個類的時候,實際上同時聲明瞭很多東西。
class Greeter{
greeting: string;
constructor(message: string){
this.greeting
}
}
把類當做接口使用如上一節裏所講,類定義會創建兩個東西:類的實例類型和一個構造函數。
因爲類可以創建出類型,所以你能夠在允許使用接口的地方使用類。
class Point{
x : number;
y : number;
}
interface Point3d extends Point{
z:number;
}
let point3d: Point3d = {x:1 ,y:2,z:3};