Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oop exercise #126

Merged
merged 8 commits into from
Aug 26, 2022
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions submissions/OlStaniev/OOP exercise/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Inhabitant {
constructor(specie, name, gender, legs, saying) {
this.specie = specie
this.name = name
this.gender = gender
this.legs = legs
this.saying = saying
}
prepareToPrint() {
return [this.specie, this.name, this.gender, this.legs, this.saying].join('; ')
}
}

class Person extends Inhabitant {
constructor(name, gender, legs, saying, hands, friends) {
super(name, gender, legs, saying)
this.hands = hands
this.friends = friends || []
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may ... send a scalar string/constant to the base class' constructor.

What experession "sends" parameters to a super's constructor?

What is super and how did you come up with using it?

prepareToPrint() {
return 'human; ' + super.prepareToPrint() + [this.hands, this.friends.join(', ')].join('; ')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We won't need to hardcode species here. We have a base class to handle it. We just need to let it know what species we deal with.

}
}

class Animal extends Inhabitant {
constructor(specie, name, gender, legs, saying, tail) {
super(specie, name, gender, legs, saying)
this.tail = tail
}
prepareToPrint() {
return super.prepareToPrint() + '; ' + this.tail + ';'
}
}

const dog = new Animal('dog', 'Bob', 'male', 4, 'woof!', 1)
const cat = new Animal('cat', 'Kitty', 'female', 4, 'meow!', 1)
const woman = new Person('Sara', 'female', 2, 'Hello!', 2)
const catWoman = new Person('Bella', 'female', 2, `${cat.saying}`, 2)
const man = new Person('Mario', 'male', 2, 'Hi!', 2, ['Bob', 'Tom', 'Eva'])

const inhabitants = [cat, dog, woman, catWoman, man]

inhabitants.forEach(inhabitant => print(inhabitant.prepareToPrint()))