Skip to content

Instantly share code, notes, and snippets.

@s1rat-dev
Created March 2, 2022 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s1rat-dev/c9ffa5725da60715236e6af33acf517b to your computer and use it in GitHub Desktop.
Save s1rat-dev/c9ffa5725da60715236e6af33acf517b to your computer and use it in GitHub Desktop.
[TYPESCRIPT] Annotations for classes.
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