-
-
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 #416
Oop exercise #416
Changes from 5 commits
bb9688f
60ab98c
f8e33ab
611b213
5f342e5
21c4ad9
c8e3d14
38a73ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
const inhabitantDefaultProperties = { | ||
species: "No species", | ||
name: "No name", | ||
gender: "No gender", | ||
saying: "Hello", | ||
friends: "", | ||
}; | ||
|
||
const humanDefaultProperties = { | ||
hands: 2, | ||
legs: 2, | ||
}; | ||
|
||
const petsDefaultProperties = { | ||
paws: 4, | ||
}; | ||
|
||
class Inhabitant { | ||
static worldPopulation = []; | ||
constructor({ | ||
species, | ||
name, | ||
gender, | ||
saying, | ||
friends, | ||
} = inhabitantDefaultProperties) { | ||
this.species = species ?? inhabitantDefaultProperties.species; | ||
this.name = name ?? inhabitantDefaultProperties.name; | ||
this.gender = gender ?? inhabitantDefaultProperties.gender; | ||
this.saying = saying ?? inhabitantDefaultProperties.saying; | ||
this.friends = friends ?? inhabitantDefaultProperties.friends; | ||
Inhabitant.worldPopulation.push(this); | ||
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. Not a good idea to populate world from within inhabitants. 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.
Can I create a separate function that will create and push objects into an array? 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. Yeah. Creating 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. I abandoned this idea because it was originally intended to simplify the creation of the world, but Later there are problems with withdrawal of friends.What method can you recommend to replace the push?It seemed to me very convenient to work initially with an empty array. ( 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. I reviewed this fragment. I agree that |
||
} | ||
|
||
addFriend({ name }) { | ||
this.friends += name + "; "; | ||
} | ||
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. From the requirements:
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. Done |
||
|
||
introduceYourSelf() { | ||
if (this.friends === "") { | ||
this.friends = "Looking for friends!"; | ||
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. What will happen if we 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. @
@OleksiyRudenko Instead of an empty array, I get a string...) Done) |
||
} | ||
return `<strong>${this.saying}! My name is ${this.name}. Species: ${this.species}. Gender: ${this.gender}. Friends: ${this.friends} </strong>`; | ||
} | ||
} | ||
|
||
class Human extends Inhabitant { | ||
constructor({ | ||
name, | ||
gender, | ||
saying, | ||
legs, | ||
hands, | ||
friends, | ||
} = humanDefaultProperties) { | ||
super({ species: "human", name, gender, saying, friends }); | ||
this.hands = hands ?? humanDefaultProperties.hands; | ||
this.legs = legs ?? humanDefaultProperties.legs; | ||
} | ||
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. Why not just to set default value the same way it is done for 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. I’ve overdone the default values a little bit. =)) 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. Done |
||
introduceYourSelf() { | ||
return ( | ||
super.introduceYourSelf() + | ||
`<strong>I have ${this.hands} hands and ${this.legs} legs!</strong>` | ||
); | ||
} | ||
} | ||
|
||
class Man extends Human { | ||
constructor({ name, saying, friends, legs, hands }) { | ||
super({ name, gender: "male", saying, legs, hands, friends }); | ||
} | ||
} | ||
|
||
class Woman extends Human { | ||
constructor({ name, saying, friends, legs, hands }) { | ||
super({ name, gender: "female", saying, legs, hands, friends }); | ||
} | ||
} | ||
|
||
class Pets extends Inhabitant { | ||
constructor({ | ||
species, | ||
name, | ||
gender, | ||
paws, | ||
saying, | ||
friends, | ||
} = petsDefaultProperties) { | ||
super({ species, name, gender, saying, friends }); | ||
this.paws = paws ?? petsDefaultProperties.paws; | ||
} | ||
introduceYourSelf() { | ||
return ( | ||
super.introduceYourSelf() + `<strong>I have ${this.paws} paws!</strong>` | ||
); | ||
} | ||
} | ||
|
||
class Cat extends Pets { | ||
static meow; | ||
constructor({ name, gender, saying = "¡miau miau", friends, paws }) { | ||
super({ | ||
species: "cat", | ||
name, | ||
gender, | ||
paws, | ||
saying, | ||
friends, | ||
}); | ||
Cat.meow = this.saying; | ||
} | ||
} | ||
|
||
class Dog extends Pets { | ||
static bark; | ||
constructor({ name, gender, saying = "¡guau guau", friends, paws }) { | ||
super({ | ||
species: "dog", | ||
name, | ||
gender, | ||
paws, | ||
saying, | ||
friends, | ||
}); | ||
Dog.bark = this.saying; | ||
} | ||
} | ||
|
||
class CatWoman extends Woman { | ||
constructor({ name, saying = "", friends, legs, hands }) { | ||
super({ name, saying, friends, legs, hands }); | ||
this.saying = Cat.meow + " " + saying; | ||
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. Use static property is a good start in the right direction. 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. @OleksiyRudenko The easiest way I see is to assign a default value to the static property of the cat:
but then all cats lose their identity and it makes me sad =))) 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. As long as property holds a primitive value the tie is weak, i.e. simply copied once. Strings, numbers and booleans are primitives. We need something that is passed by reference. These are JS objects (arrays and functions are also objects). Class methods can also be static. There are also very specific methods - getters and setters. 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. @OleksiyRudenko Tried to bind Woman and cat by static method. Looks strange) 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. It does. Truth is that hybrids do not fit OOP hierarchical nature really well. 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. @OleksiyRudenko Good task. At a glance nothing difficult, but on the small details forces to revise a lot of materials). Do I still have to make some adjustments to the task? |
||
} | ||
} | ||
|
||
const manJose = new Man({ name: "José", saying: "Hola, amigo" }); | ||
const womanMartina = new Woman({ name: "Martina", saying: "Buenos días" }); | ||
const catLalo = new Cat({ name: "Lalo", gender: "female" }); | ||
const dogPako = new Dog({ name: "Pako", gender: "male" }); | ||
const catwomanNerea = new CatWoman({ name: "Nerea", saying: "muchachos" }); | ||
|
||
manJose.addFriend(catwomanNerea); | ||
womanMartina.addFriend(dogPako); | ||
catwomanNerea.addFriend(catLalo); | ||
catwomanNerea.addFriend(manJose); | ||
|
||
function showWorldPopulation() { | ||
const inhabitantNames = Inhabitant.worldPopulation.map( | ||
(inhabitant) => inhabitant.name | ||
); | ||
print( | ||
`<strong>Population: ${Inhabitant.worldPopulation.length} inhabitants! It is: ${inhabitantNames}</strong>` | ||
); | ||
Inhabitant.worldPopulation.forEach((inhabitant) => | ||
print(inhabitant.introduceYourSelf()) | ||
); | ||
} | ||
|
||
showWorldPopulation(); |
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.
It would be a better approach to have defaults within class definitions. Not necessarily in form of explicit const definitions. KISS
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.
done