Classes in Typescript
Basic structure of the class, modifier usage etc..
class Vehicle {
public move(): void {
console.log('gogogogo');
}
private stop(): void {
console.log('stopped');
}
public performStop() {
this.stop();
}
constructor(public speed: number) {}
//public modifier for parameter automaticaly does the same as below
// speed: number;
// constructor(speed: number) {
// this.speed = speed;
// }
static type: string = 'vehicle';
static printType() {
console.log(VehicleClass.type);
}
}
class Car extends Vehicle {
constructor(public wheels: number, color: string) {
// don't add public modifier to color because we don't want to reassign color property of Vehicle class
super(color);
}
public move(): void {
console.log('vroom');
}
}
let car = new Car(4, 'black');