Skip to content

Instantly share code, notes, and snippets.

@MadaShindeInai
Last active April 24, 2022 15:49
Show Gist options
  • Save MadaShindeInai/8b9c849bf4d1b1dff69f65c76f313ba1 to your computer and use it in GitHub Desktop.
Save MadaShindeInai/8b9c849bf4d1b1dff69f65c76f313ba1 to your computer and use it in GitHub Desktop.
Complete class defenition/description using Typescript
// interface ensure class instance shape. Notice that the interface includes the parameters of the constructor, not the properties
interface Vehicle {
make: string;
color: string;
doors: number;
accelerate(speed: number): string;
brake(): string;
turn(direction: 'left' | 'right'): string;
}
class Car implements Vehicle{
// Properties
private _make: string;
private _color: string;
private _doors: number;
// Constructor
constructor(make: string, color: string, doors = 4) {
this._make = make;
this._color = color;
if ((doors % 2) === 0) {
this._doors = doors;
} else {
throw new Error('Doors must be an even number');
}
}
// Accessors
get make() {
return this._make;
}
set make(make) {
this._make = make;
}
get color() {
return 'The color of the car is ' + this._color;
}
set color(color) {
this._color = color;
}
get doors() {
return this._doors;
}
set doors(doors) {
if ((doors % 2) === 0) {
this._doors = doors;
} else {
throw new Error('Doors must be an even number');
}
}
// Methods
accelerate(speed: number): string {
return `${this.worker()} is accelerating to ${speed} MPH.`
}
brake(): string {
return `${this.worker()} is braking with the standard braking system.`
}
turn(direction: 'left' | 'right'): string {
return `${this.worker()} is turning ${direction}`;
}
// This function performs work for the other method functions
protected worker(): string {
return this._make;
}
}
let myCar1 = new Car('Cool Car Company', 'blue');
class ElectricCar extends Car {
// Properties unique to ElectricCar
private _range: number;
// Constructor
constructor(make: string, color: string, range: number, doors = 2) {
super(make, color, doors);
this._range = range;
}
// Accessors
get range() {
return this._range;
}
set range(range) {
this._range = range;
}
// Methods
charge() {
console.log(this.worker() + " is charging.")
}
brake() {
return `${this.worker()} is braking with the regenerative braking system.`
}
}
let spark = new ElectricCar('Spark Motors','silver', 124, 2);
let eCar = new ElectricCar('Electric Car Co.', 'black', 263);
console.log(eCar.doors);
spark.charge();
console.log(spark.brake());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment