James
Active Coder
Now, this is a massive step from the last tutorial, you should know the basics of python to understand this.
This tutorial will be going into Object Oriented Programming.
How do I define a class?
To define a class you need to type class along with the name, similar to a function it must also be followed by ():
Okay, well you've defined the class, but what now? This is basically useless at this point but we can do much more.
We can make things like players, enemies, shopping carts even properties of something.
So, what else can we do?
Well, classes have things called constructors the one we will mainly use in this tutorial is
Init stands for initiation, or, whenever the class is created
When creating a class it is called an Object.
So, this is how we use the __init__ constructer, it's the same as defining a function but this function is private as it starts with __
self is the object being created.
Although we have the __init__ it is still basically useless.
What we are going to do is assign some variables to the object.
Now the object being created has some variables,
yet this is still useless as we haven't created the object, let's do that now.
Now Test1 is an object.
It does nothing, but it can now be useful.
We can change the variables for the object by simply typing
We can also create multiple objects,
Each object is using the same class but we have set different variables.
This can be useful for making things such as shopping carts as you can have multiple shopping cart objects.
We can do much more with classes, in this tutorial I will only show you one other thing,
We can have functions to do things in the object, to do so we define the function as normal,
To run this function we can type,
This function does nothing,
So lets make it change the age.
Now if you run this it will change the age from 50 to 500.
That is all for this tutorial.
I hope this helped you!
This tutorial will be going into Object Oriented Programming.
How do I define a class?
To define a class you need to type class along with the name, similar to a function it must also be followed by ():
class TestClass():
Okay, well you've defined the class, but what now? This is basically useless at this point but we can do much more.
We can make things like players, enemies, shopping carts even properties of something.
So, what else can we do?
Well, classes have things called constructors the one we will mainly use in this tutorial is
__init__
Init stands for initiation, or, whenever the class is created
When creating a class it is called an Object.
Python:
class TestClass():
def __init__(self):
pass
self is the object being created.
Although we have the __init__ it is still basically useless.
What we are going to do is assign some variables to the object.
Python:
class TestClass():
def __init__(self):
self.name = "Hello"
self.age = 50
self.health = 100
yet this is still useless as we haven't created the object, let's do that now.
Python:
class TestClass():
def __init__(self):
self.name = "Hello"
self.age = 50
self.health = 100
Test1 = TestClass()
It does nothing, but it can now be useful.
We can change the variables for the object by simply typing
Test1.(variablename) = (newvalue)
We can also create multiple objects,
Python:
class TestClass():
def __init__(self):
self.name = "Hello"
self.age = 50
self.health = 100
Test1 = TestClass()
Test1.name = "Test1"
Test1.health = 500
Test2 = TestClass()
Test2.name = "Test2"
Test2.health = 250
print(Test1.name,Test1.health)
print(Test2.name,Test2.health)
This can be useful for making things such as shopping carts as you can have multiple shopping cart objects.
We can do much more with classes, in this tutorial I will only show you one other thing,
We can have functions to do things in the object, to do so we define the function as normal,
Python:
class TestClass():
def __init__(self):
self.name = "Hello"
self.age = 50
self.health = 100
def changeage(self):
pass
Test1 = TestClass()
Test1.changeage()
This function does nothing,
So lets make it change the age.
Python:
class TestClass():
def __init__(self):
self.name = "Hello"
self.age = 50
self.health = 100
def changeage(self,newage):
self.age = newage
Test1 = TestClass()
print(Test1.age)
Test1.changeage(500)
print(Test1.age)
That is all for this tutorial.
I hope this helped you!