Contains data (variables (attributes)) and code (functions (methods))
Define the Person class.
python
class Person():
def __init__(self, name):
self.name=name
hunter=Person('Reiner Tonnies')
print('The mighty hunter:', hunter.name) # The mighty hunter: Reiner Tonnies
__init () __ ()
is a method that initializes individual objects when they are created from the class definition. (Something like a constructor)self
.python
Class Car():
def exclaim(self):
print("I'm a Car!")
Class Yugo(Car):
def exclaim(self):
print("I'm a Yugo!")
def need_a_push(self):
print("Hi!")
give_me_a_car=Car()
give_me_a_yugo=Yugo()
#override
give_me_a_car.exclaim() #I'm a Car!
give_me_a_yugo.exclaim() #I'm a Yugo!
#Add method
give_me_a_yugo.need_a_push() #Hi!
give_me_a_car.need_a_push() #error
Recommended Posts