Skip to content

Instantly share code, notes, and snippets.

@SirSerje
Created October 25, 2021 14:33
Show Gist options
  • Save SirSerje/fb52c2e4e04eea752933caf6dc9fd99b to your computer and use it in GitHub Desktop.
Save SirSerje/fb52c2e4e04eea752933caf6dc9fd99b to your computer and use it in GitHub Desktop.
Typescript example or how you can use OOP on more object oriented languages, like C / Java
console.clear() //just clear console
//any instantinated dog should bark somehow
interface IBark {
bark: (param: any) => string
}
// abstract for class means, class should be extended, not instantinated
abstract class AbstractDog implements IBark {
private weight: number = 15;
private static barkSound = 'woof'
constructor(weight:number) {
this.weight = weight
}
bark() {
return AbstractDog.barkSound
}
}
class DogContainer {
protected dogCarry:Array<AbstractDog>;
constructor() {
this.dogCarry = []
}
add(dog: AbstractDog):void {
this.dogCarry.push(dog)
}
makeWoof = (): string => this.dogCarry.reduce((acc: string, i:AbstractDog) => `${acc} ${i.bark()}`, '')
}
//https://www.google.com/search?q=doggo&sxsrf=AOaemvKMLy0F3zCzQrMQf7VGu0dT96w7QA:1635170980997&tbm=isch&source=iu&ictx=1&fir=VUd1sFMDlvXkAM%252C_4ql3r9FbziLKM%252C_%253BDVBCI9SCcY7rfM%252CuTU0yC5-PqScJM%252C_%253BWL_TbxROIaO_PM%252CnAGjKfWmJmTt6M%252C_%253BFM4uiZwWAdSmhM%252CAKadFRLDi2MFEM%252C_%253Boxt8Ga6WSjY6oM%252CLe5EPlmaXDptcM%252C_%253Bvx4OL7WuPm878M%252CqhLl3U6c3CTcdM%252C_%253Bm3385ZlXnmBhrM%252CtofIqjXtsPz6PM%252C_%253BkmHGxanSpmlaHM%252C7Zf7aC6kdW9MNM%252C_%253BJ6xRyKA5tdRiiM%252CguAv4du-Ka5eEM%252C_%253B-YzN9tkBg1RvLM%252CNyh-0w4jLi9o9M%252C_%253BQYhbnDIis0GadM%252Co2i1s_eGFK308M%252C_%253BsqI11uISHOR4gM%252CKsKbkcSnZvQSAM%252C_%253B6PbL3BK5krGiIM%252C4DQri05Ky3ZPgM%252C_%253BNWd_c0WX-lv02M%252CYKi3ebUCXio9-M%252C_&vet=1&usg=AI4_-kSgMrpS-QQhmtjojbku5L6l_2q7OQ&sa=X&ved=2ahUKEwi8luDL3uXzAhVriOAKHduLBHsQ9QF6BAgWEAE#imgrc=VUd1sFMDlvXkAM
class Doggo extends AbstractDog {
setWeight() {
// this.weight = 30;
}
}
// const aDog = new AbstractDog(13)
const doggo = new Doggo(15)
const doggoClone = new Doggo(13)
const container = new DogContainer()
container.add(doggo)
container.add(doggoClone)
console.log(container.makeWoof())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment