Created
March 2, 2022 18:29
-
-
Save s1rat-dev/c9ffa5725da60715236e6af33acf517b to your computer and use it in GitHub Desktop.
[TYPESCRIPT] Annotations for classes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vehicle { | |
public drive(): void { | |
console.log('chugga chugga'); | |
} | |
public honk(): void { | |
console.log('beep'); | |
} | |
} | |
const vehicle = new Vehicle(); | |
vehicle.drive(); | |
vehicle.honk(); | |
// Inheritance | |
class Car extends Vehicle { | |
// Constructor | |
constructor(private readonly name: string) { | |
super(); | |
this.name = name; | |
} | |
public logName(): void { | |
console.log(`Hello i'm ${this.name}`); | |
} | |
} | |
const car = new Car('Honda'); | |
car.honk(); | |
car.drive(); | |
car.logName(); | |
// Fields with inheritance | |
class Person { | |
constructor(private name: string, | |
private surname: string) { | |
} | |
public getName = (): string => this.name; | |
public getSurname = (): string => this.surname; | |
} | |
class Student extends Person { | |
constructor(name: string, | |
surname: string, | |
private studentNumber: number) { | |
super(name,surname); | |
} | |
public getStudentNumber = (): number => this.studentNumber; | |
} | |
const s1rat = new Student("Sırat","Çöp",383221); | |
console.log(`${s1rat.getName()} - ${s1rat.getSurname()} - ${s1rat.getStudentNumber()}`); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment