Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python


Chapter Nine: The Inheritance Class


Download 1.25 Mb.
Pdf ko'rish
bet40/48
Sana30.01.2023
Hajmi1.25 Mb.
#1140552
1   ...   36   37   38   39   40   41   42   43   ...   48
Bog'liq
Coding in Python A Comprehensive Beginners Guide to Learn the Realms

Chapter Nine: The Inheritance Class
Now that you have learned how to write a class, it is pertinent to mention that
Python classes are well known for the ease of use they offer to programmers.
Once you have written a class, you can reuse it multiple times. There is a
process called inheritance in which a subclass is inherited from the parent
class. The inherited class is named that way because it inherits the attributes
of the parent class. The inherited class is dubbed as the child class. It can use
each attribute and method of the parent class. However, you also can create
new attributes only for the child class.
Just as you did for the parent class, you will also have to use the __init__()
method for the child class. I will create a child class of racer bikes.
class Bike():
"""This class will build the model of a bike."""
def __init__(self, bmodel, bmake, bcolor, byear):
self.bmodel = bmodel
self.bmake = bmake
self.bcolor = bcolor
self.byear = byear
self.odometer_reading = 0
def fullname(self):
fullbikename = "We have a bike that hit the markets in " +
str(self.byear) + ". The model is " + self.bmodel + ". The bike is
manufactured by " + self.bmake + ". Its color is " + self.bcolor + "."
return fullbikename.title()
def reading_odometer(self):
print("This bike has run " + str(self.odometer_reading) + " kilometers
on the road.")


def updating_the_odometer(self, bmileage):
self.odometer_reading = bmileage
if bmileage >= self.odometer_reading:
self.odometer_reading = bmileage
else:
print("You are not authorized to roll back the reading of the
odometer.")
def incrementing_odometer(self, bmileage):
self.odometer_reading += bmileage
class RacerBike(Bike):
def __init__(self, bmodel, bmake, bcolor, byear):
super().__init__(bmodel, bmake, bcolor, byear)
racer1 = RacerBike('URS: Gravel Riding', 'BMC', 'Grey', '2017')
print(racer1.fullname())
racer2 = RacerBike('Trackmachine', 'BMC', 'Blue', '2015')
print(racer2.fullname())
racer3 = RacerBike('Alpenchallenge', 'BMC', 'Red', '2012')
print(racer2.fullname())
= RESTART: C:/Users/saifia computers/Desktop/sample.py
We Have A Bike That Hit The Markets In 2017. The Model Is Urs: Gravel
Riding. The Bike Is Manufactured By Bmc. Its Color Is Grey.
We Have A Bike That Hit The Markets In 2015. The Model Is
Trackmachine. The Bike Is Manufactured By Bmc. Its Color Is Blue.
We Have A Bike That Hit The Markets In 2015. The Model Is
Trackmachine. The Bike Is Manufactured By Bmc. Its Color Is Blue.
>>>
The most important thing to keep in mind while creating a child class is to


keep the child class inside the parent class. The name of the child class must
include parenthesis that carry the name of the parent class. I have added one
additional function, the super function that aids Python in forming
connections between the child class and the parent class. The child class has
taken all the attributes of the parent class. I have not yet added any special
attribute to the racer bike.

Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   ...   36   37   38   39   40   41   42   43   ...   48




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