Skip to content

Instantly share code, notes, and snippets.

@miladvafaeifard
Created October 23, 2018 18:34
Show Gist options
  • Save miladvafaeifard/52a88a7f18c571a03036eb79920b8d6d to your computer and use it in GitHub Desktop.
Save miladvafaeifard/52a88a7f18c571a03036eb79920b8d6d to your computer and use it in GitHub Desktop.
Liskov Substitution Principal
abstract class Vehicle {
protected speed: number;
protected cubicCapacity: any;
constructor(speed: number, cubicCapacity: number){
this.speed = speed;
this.cubicCapacity = cubicCapacity;
}
public getSpeed(): number {
return this.speed;
}
public getCubicCapacity(): number {
return this.cubicCapacity
}
public abstract speedUp(): void;
}
class Car extends Vehicle {
private hatchedBack: any;
constructor(){
super(100, 10);
this.hatchedBack = false;
}
public isHatchBack(): boolean {
return this.hatchedBack
}
public speedUp(): void {
this.speed += 10;
}
}
class Bus extends Vehicle {
private emergencyExitLocation: string;
constructor(){
super(60, 50);
this.emergencyExitLocation = 'Budapest, Hosok ter';
}
public getEmergencyExitLocation(): string {
return this.emergencyExitLocation;
}
public speedUp(): void {
this.speed += 5;
}
}
const car: Vehicle = new Car();
const bus: Vehicle = new Bus();
console.log(`speed of car is ${car.getSpeed()}`);
console.log(`capacity of car is ${car.getCubicCapacity()}`);
car.speedUp();
car.speedUp();
car.speedUp();
console.log(`speed of car is ${car.getSpeed()}`);
console.log('\n');
console.log(`speed of bus is ${bus.getSpeed()}`);
console.log(`capacity of bus is ${bus.getCubicCapacity()}`);
bus.speedUp();
bus.speedUp();
console.log(`speed of bus is ${bus.getSpeed()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment