Classes
A class is like a blueprint for creating objects, it brings out the OOP concepts into view. An object in class in return has properties and methods associated with it.
To create a class in python we use the keyword class i.e
class Me:
def __init__(self, name, age, hobby):
self.name = name
self.age = age
self.hobby = hobby
In the above example, we have created a class called Me with a function.
The next step will be to have values in the object Me
Billy = Me('Billy', 50, 'dancing')
As you can use we've used the exact order as we specified when creating the object, the whole code will now look like this:
class Me:
def __init__(self, name, age, hobby):
self.name = name
self.age = age
self.hobby = hobby
billy = Me('Billy', 50, 'dancing')
print(billy.age)
When we run this we will get the age of billy which is 50, it is because we only called for the age to be printed.
We can also add another method in our class so that we get a nice output
class Me:
def __init__(self, name, age, hobby):
self.name = name
self.age = age
self.hobby = hobby
def message(self):
return f'My name is {self.name} ,i like {self.hobby} and i am {self.age}'
billy = Me('Billy', 50, 'dancing')
print(billy.message())
This will output the message, name and hobby of billy i.e
My name is Billy ,i like dancing and i am 50
We can multiple methods in a class. For example what if we want our age to add after a year, how will we put it....
Hmmmmmm, thinking, yeah i know how we will do it. We will add another method that checks if one year has passed...
class Me:
def __init__(self, name, age, hobby):
self.name = name
self.age = age
self.hobby = hobby
def message(self):
return f'My name is {self.name} ,i like {self.hobby} and i am {self.age}'
def one_year_passed(self):
self.age +=1
billy = Me('Billy', 50, 'dancing')
billy.one_year_passed()
print(billy.message())
In the previous example it printed out the age as 50, but when we run the above example the age will change from 50 to 51 because we called the one_year_passed method before printing and the method did add 1 to the age which we had before
Now that we know how to create a class, how do we create another class that extends the class we had earlier. To do that, we simply create another and pass the other class name as a parameter.
So let's create another class 'Friend' that has all the properties of 'Me' and then we add a method my_debt in the Friend class.
Them what we want to do again is overwrite the method message() so that the class Friend has it's own message() method that also prints out the debt.
class Me:
def __init__(self, name, age, hobby):
self.name = name
self.age = age
self.hobby = hobby
def message(self):
return f'My name is {self.name} ,i like {self.hobby} and i am {self.age}'
def one_year_passed(self):
self.age +=1
#Extend class
class Friend(Me):
def __init__(self, name, age, hobby):
self.name = name
self.age = age
self.hobby = hobby
self.debt = 0
def my_debt(self, debt):
self.debt = debt
def message(self):
return f'My name is {self.name} ,i like {self.hobby} and i am {self.age} and my debt is {self.debt}'
# Initialising Me object
billy = Me('Billy', 50, 'dancing')
# Initialising Friend object
dan = Friend ('Dan', 20, 'singing'
dan.my_debt(1000)
print(dan.message())
billy.one_year_passed()
print(billy.message())
When we run that, we will have an output as shown below:
My name is Dan, i like singing and i am 20 and my debt is 1000
My name is Billy, i like dancing and i am 51
That's all for now, see you later..
Top comments (0)