What is a class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is a class?

i know the technical definitions, but i can't really understand it

28th Sep 2018, 7:56 PM
Kryoblaze
Kryoblaze - avatar
3 Answers
+ 5
classes shouldn't be viewed as objects, objects are created from classes
28th Sep 2018, 10:14 PM
D_Stark
D_Stark - avatar
+ 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.
29th Sep 2018, 3:34 AM
Gordon
Gordon - avatar
+ 1
A class is an object that stores data you can make instances of, and use it's functions.
28th Sep 2018, 8:11 PM
Lil Taco
Lil Taco - avatar