In this example, we would add some more methods (functions) to our person class. We would add the methods to walk and speak such that it can be invoked when we run the code as shown below.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def walk(self):
print(self.name + ' is walking...')
def speak(self):
print('Hello my name is ' + self.name + ' and I am ' + str(self.age) + ' years old')
ali = Person('Ali', 22)
jay = Person('Jay', 18)
print(ali.name + ' ' + str(ali.age))
ali.speak()
ali.walk()
print(jay.name + ' ' + str(jay.age))
jay.speak()
jay.walk()
When we
run the code we have the following output:
Ali 22
Hello my name is Ali and I am 22 years old
Ali is walking...
Jay 18
Hello my name is Jay and I am 18 years old
Jay is walking...
No comments:
Post a Comment