+ 1
What is the main reason for OOP?
Main Reason For OOP(Object Oriented Programming)? Please have honest answers, ty for the answers!đđđ
3 RĂ©ponses
+ 2
You would find the uses of OOP if you have worked with languages like C or lua.
OOP allows you to make classes which act as the blueprint for a certain object. For example think you are making a game with zombies. Each zombie has properties like health, damage, favouriteFood(maybe brainsđ) etc.
So you just start to code those zombies on scratch. Let me try it in python - because I cant even think about java without OOP.
zombie1 = {health:100,damage:85,favFood:"brains"}
zombie2 = {health:10,damage:65,favFood:"blood"}
This will be OK if you just going to deal with only these 2 zombies. But the player wants more zombies. So we have to code 1000 of xombies to satisfy the user. And also the zombie should have its behaviours like walk, eat, attack, poop, etc.
But classes makes the task more easier and efficient.
To be continued....
+ 2
So we can make a class of zombies like this.
class Zombie:
def __init__(self,health,damage):
self.heath = health
self.damage = damage
def attack(self,opp):
opp.health = opp.health - self.damage
#and then storing our zombies
to
zombieTruck = []
for x in range(100000):
zombieTruck.append(Zombie())
0
It allows you to keep your code in a tight little package. Make a car put all variables and methods that are for a car all in a neat little block called class Car. It is a blueprint builder. So you will always have a working car and the plans for a new one. It is a nice structure easy to create multiple objects etc.