+ 1
What is a class?
i know the technical definitions, but i can't really understand it
4 ответов
+ 5
classes shouldn't be viewed as objects, objects are created from classes
+ 4
When do you use function? When you want to redo the operation again and again.
Similarly, when do you use class? You are right, when you want to call the same property again and again.
For example, if you are coding a rpg game, and you have many heroes and monsters. and when they hit each other (hero vs monster or monster vs hero, but of course you can also hero vs hero if you like), they will be "hurt", in computer, a variable storing their health point decrement. Now if you handle them separately, declare a new hp variable for each hero, it can store hp too, but when you want to record the decrement, imagine what you have to do, you have to write a switch-case, meaning a lot of if: if this hero, this_hero_hp-- elseif that hero, that_hero_hp-- , you have to manually link up the relationship between each hero and their hp. but with class, it's like this :
class characters
self.hp = 10
characters Kryoblaze
character Gordon_Chan
if (hit)
Gordon_Chan.hp-=10
See? the hp property is built-in and linked.
+ 1
A class is an object that stores data you can make instances of, and use it's functions.