Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet118/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   114   115   116   117   118   119   120   121   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Method Overriding


Sometimes when using inheritance, you want the subclass to be able to perform a different action when the same method is called. Take our makeSound method from the previously created Animal class. It prints out “roar”, but that’s not the sound you want dogs making when you create your Dog class. Instead, we use the concept of method overriding to change what the method does. Within the subclass, we redefine the method (with the same name) to perform the task differently. Python will always use the method defined within the subclass first, and if one doesn’t exist, then it will check the superclass. Let’s use method overriding to alter the makeSound method and print the proper statement for our Dog class:

1| # overriding methods defined in the superclass 3| class Animal( ):
4| def makeSound(self):
5| print("roar") 7| class Dog(Animal):
8| def makeSound(self):
9| print("bark")
11| sam, lion = Dog( ), Animal( ) # declaring multiple variables on a single line
13| sam.makeSound( ) # overriding will call the makeSound method in Dog
15| lion.makeSound( ) # no overriding occurs as Animal does not inherit anything

Go ahead and run the cell. On the 8th line, we declare two instances sam and lion. The next line is where we call the makeSound method from our dog instance of sam. The output results in “bark” because of method overriding. As the method was inherited, but then redefined within the Dog class, it prints bark instead. On the 10th line, we call the same method with our Animal instance lion. This output is “roar” because lion is an instance of the Animal class. Remember that inheritance does not work backward.
Subclasses cannot give superclasses any features.

Inheriting Multiple Classes


Thus far, we’ve seen how we can inherit from a single superclass. Now we’re going to try inheriting from multiple classes. The main difference is how you super the attributes. Rather than using the super method, you call the class name directly and pass in the self parameter with the attributes. Let’s see how:

1| # how to inherit multiple classes 3| class Physics( ):
4| gravity = 9.8 6| class Automobile( ):
7| def __init__(self, make, model, year):
8| self.make, self.model, self.year = make, model, year # declaring all attributes on one line
10| class Ford(Physics, Automobile): # able to access Physics and Automobile attributes and methods 11| def __init__(self, model, year):
12| Automobile.__init__(self, "Ford", model, year) # super does
not work with multiple
14| truck = Ford("F-150", 2018)
16| print(truck.gravity, truck.make) # output both attributes

Go ahead and run the cell. We’ll get an output of 9.8 and “Ford”. On line 7 you’ll notice that we inherit two classes within the parenthesis for the Ford class. The 9th line is where the magic occurs this time though. Instead of using super, we initialize the variables by calling the name of the inherited class directly. Using the init method, we pass the self parameter along with all the attributes that Automobile requires. Python knows which superclass to use because of the name at the beginning of the line. On the last line, we’re able to see that we have access to both attributes declared within Physics and Automobile, where we are inheriting from.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   114   115   116   117   118   119   120   121   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish