-
-
Notifications
You must be signed in to change notification settings - Fork 184
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
oop exercise #126
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class Inhabitants { | ||
constructor(name, gender, legs, saying) { | ||
this.name = name; | ||
this.gender = gender; | ||
this.legs = legs; | ||
this.saying = saying | ||
} | ||
|
||
prepareToPrint() { | ||
return [this.name, this.gender, this.legs, this.saying].join(';') | ||
} | ||
} | ||
|
||
class People extends Inhabitants { | ||
constructor(name, gender, legs, hands, saying) { | ||
super(name, gender, legs, saying) | ||
this.hands = hands | ||
} | ||
prepareToPrint() { | ||
return [this.name, this.gender, this.legs, this.hands, this.saying].join(';') | ||
} | ||
} | ||
|
||
class PeopleWithFriends extends People { | ||
constructor(name, gender, legs, hands, saying, friends) { | ||
super(name, gender, legs, hands, saying) | ||
this.friends = friends | ||
} | ||
prepareToPrint() { | ||
return [this.name, this.gender, this.legs, this.hands, this.saying, this.friends].join(';') | ||
} | ||
} | ||
|
||
const dog = new Inhabitants('Bob', 'male', 4, 'woof!') | ||
const cat = new Inhabitants('Kitty', 'female', 4, 'meow!') | ||
const woman = new People('Sara', 'female', 2, 2, 'Hello!') | ||
const catWoman = new People('Bella', 'female', 2, 2, `${cat.saying}`) | ||
const man = new PeopleWithFriends('Mario', 'male', 2, 2, 'Hi!', ['Bob', 'Tom', 'Eva']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By convention class name should be in singular form unless it holds multiple entitites indeed. No need to have Imagine we want to specify neighbours, pets etc (and any such list may be empty or undefined). This should be handled by a single class. |
||
|
||
const inhabitants = [cat, dog, woman, catWoman, man] | ||
|
||
inhabitants.forEach(inhabitant => print(inhabitant.prepareToPrint())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is way to re-use parent class method. It makes sense as this helps to follow SOLID principles. Check the supplementary materials on the task's README.