Typescript-6.類
阿新 • • 發佈:2019-01-09
類:
在ts裡,我們可以使用常用的面向物件模式。
基於類的設計模式中一種最基本的模式是允許使用繼承來擴充套件現有的類。
預設為public
protected子類可以訪問
你可以使用readonly關鍵字將屬性設定為只讀的。只讀屬性必須在宣告時或者建構函式裡被初始化
引數屬性
在上面的例子中,我們不得不定義一個受保護的成員name和一個建構函式引數theName在Person類裡,並且立刻將theName的值付給name。
抽象類做為其它派生類的基類使用。他們一般不會直接被例項化。
不同於介面,抽象類可以包含成員的實現細節。
建構函式
當你在TypeScript裡聲明瞭一個類的時候,實際上同時聲明瞭很多東西。
如上一節裡所講,類定義會建立兩個東西:類的例項型別和一個建構函式。
因為類可以創建出型別,所以你能夠在允許使用介面的地方使用類。
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};