Classes and objects

7 minutes
Share the link to this page
Copied
  Completed

Transcript

Thank you for coming back to the course. In this lesson, we are going to talk about objects. Objects are very important concept, and is also very important in building our hundred game. Everything is an object. And to create an object, we need to first define a class. Cast is now a grouping of objects.

With this grouping, we can create as many objects as we want. First, we can define a class by using the class keyword. And then inside the class definition, we use def. to defy our initialization function. You can call it the constructor function as well. We put in parameter light, self, name, and level for the initialization and then we assigned self dot name equals to name self taught never equals to Navajo.

So what we have done here is to define a class. A class again, can be considered as a blueprint. And we have this grouping, we can create a which object, which is essential for our game. So say for example, when we create an object first which we say this first which object is going to be instantiated from the class, which with the parameter of name first which, and never want. The syntax we use here is actually referring to the initialization function. The initialization function takes three parameters.

The first one self is referring to the first wish variable The second prime is name is referring to the string first which the first parameter level is referring to the one that means, this which is with level one. And then we assign the first wish, dot name equals to the string first which and the second night here we assign the level of this first which to be one. So, after that, when we try to print now first which dot name, we will get first which, and if you clean first which don't novel, we will get one. So, this first which here is an object instantiate from the wish class, if you want to create an other object from the which class Say the second which this time, we can pass in some other parameters. For example, the name will be second witch and the level will be five.

Now we have a witch, that with a name second witch and with the level of level five. Now, we can see that we have successfully created two objects, the first witch and the second which and there is another way to use this class. First, we try to go back to our definition of the class by using Ctrl P. And then, we define a function, which is our EPR is a soft form of representation. This function takes one parameter we put in self and then inside the function will return correct blank cat. And then therefore, curly bracket. And we will replace the first career blanket with the self taught name and the second career blanket with the self taught level and then we will recreate the first which object by declaring that as a witch with two parameters and we print that out.

You will see that this time, it will tell us the name of this which object First which the level of this which object is one that's why we call the first which object we can immediately know the name and the level of that is all because of the representation function. Next, we are going to import the Windham package, which is important to create the attack function for our which. And then we define the attack function. Which craft underscore attack. And we pass in self as a parameter. Inside the function, we will return when them when int and then with parameter one to 10.

We will then multi-part this attack Where do with the level of the witch Now with this new class definition, we have to instantiate the object again. Using the same index, we pass in first which as the name of the object, and one as the level. Now we call first which don't witch craft attack. And we can see that we have an attack of seven. So let's recap what we have done here. When we create the object first which we are using the class, which in this class, which has an initialization function, with three parameters, the first which will be passed to the self, and then a string first which we passed to the name and then the integer one will pass to the level After that, we assigned the name of the object to be the name.

And then we assigned the level of the object to be the level. Next, we call the wage craft attack function. This function will generate a random number from one to 10. And then according to the level of the switch, it will multiply this level value to the random number. Because the first which is with level one, so the attack is not really that damaging. But imagine if we have a rich with level five, the damage is going to be big.

So that's all about objects and see you in our next class.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.