Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python
Overriding Methods from Parent Class
Download 1.25 Mb. Pdf ko'rish
|
Coding in Python A Comprehensive Beginners Guide to Learn the Realms
Overriding Methods from Parent Class
You can override methods from the parent class by defining a method with the same name as in the parent class. Python will then block the method's execution from the parent class, allowing the child class to override the method. 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(RacerBike,self).__init__(bmodel, bmake, bcolor, byear) self.performance_tire = 'three textile layered' self.aerodynamic_efficiency = 'better lift/drag ratio' def describe_tires(self): print("This racer bike has " + self.performance_tire + " performance tires.") def describe_aerodynamics(self): print("This racer bike has " + self.aerodynamic_efficiency + " for improved aerodynamic efficiency.") def incrementing_odometer(self, bmileage): print("I cannot increment the odometer.") racer1 = RacerBike('URS: Gravel Riding', 'BMC', 'Grey', '2017') print(racer1.fullname()) racer1.describe_tires() racer1.describe_aerodynamics() racer1.reading_odometer() racer1.incrementing_odometer(1000) racer2 = RacerBike('Trackmachine', 'BMC', 'Blue', '2015') print(racer2.fullname()) racer2.describe_tires() racer2.describe_aerodynamics() racer2.reading_odometer() racer2.incrementing_odometer(1000) racer3 = RacerBike('Alpenchallenge', 'BMC', 'Red', '2012') print(racer2.fullname()) racer3.describe_tires() racer3.describe_aerodynamics() racer3.reading_odometer() racer3.incrementing_odometer(5000) = 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. This racer bike has three textile layered performance tires. This racer bike has better lift/drag ratio for improved aerodynamic efficiency. This bike has run 0 kilometers on the road. I cannot increment the odometer. We Have A Bike That Hit The Markets In 2015. The Model Is Trackmachine. The Bike Is Manufactured By Bmc. Its Color Is Blue. This racer bike has three textile layered performance tires. This racer bike has better lift/drag ratio for improved aerodynamic efficiency. This bike has run 0 kilometers on the road. I cannot increment the odometer. We Have A Bike That Hit The Markets In 2015. The Model Is Trackmachine. The Bike Is Manufactured By Bmc. Its Color Is Blue. This racer bike has three textile layered performance tires. This racer bike has better lift/drag ratio for improved aerodynamic efficiency. This bike has run 0 kilometers on the road. I cannot increment the odometer. >>> An interesting about classes is the flexibility they have to offer. For example, instead of making the performance tires an attribute, we can turn it into a separate class. After that we can add as many instances to this new class as we want to. 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 Performancetires(): def __init__(self, performance_tire= 'three textile layered'): self.performance_tire = performance_tire def describe_performancetires(self): print("This racer bike has " + self.performance_tire + " performance tires.") class RacerBike(Bike): def __init__(self, bmodel, bmake, bcolor, byear): super(RacerBike,self).__init__(bmodel, bmake, bcolor, byear) self.performancetires = Performancetires() self.aerodynamic_efficiency = 'better lift/drag ratio' def describe_aerodynamics(self): print("This racer bike has " + self.aerodynamic_efficiency + " for improved aerodynamic efficiency.") def incrementing_odometer(self, bmileage): print("I cannot increment the odometer.") racer1 = RacerBike('URS: Gravel Riding', 'BMC', 'Grey', '2017') print(racer1.fullname()) racer1.describe_aerodynamics() racer1.reading_odometer() racer1.incrementing_odometer(1000) racer1.performancetires.describe_performancetires() racer2 = RacerBike('Trackmachine', 'BMC', 'Blue', '2015') print(racer2.fullname()) racer2.describe_aerodynamics() racer2.reading_odometer() racer2.incrementing_odometer(1000) racer2.performancetires.describe_performancetires() racer3 = RacerBike('Alpenchallenge', 'BMC', 'Red', '2012') print(racer2.fullname()) racer3.describe_aerodynamics() racer3.reading_odometer() racer3.incrementing_odometer(5000) racer3.performancetires.describe_performancetires() = 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. This racer bike has better lift/drag ratio for improved aerodynamic efficiency. This bike has run 0 kilometers on the road. I cannot increment the odometer. This racer bike has three textile layered performance tires. We Have A Bike That Hit The Markets In 2015. The Model Is Trackmachine. The Bike Is Manufactured By Bmc. Its Color Is Blue. This racer bike has better lift/drag ratio for improved aerodynamic efficiency. This bike has run 0 kilometers on the road. I cannot increment the odometer. This racer bike has three textile layered performance tires. We Have A Bike That Hit The Markets In 2015. The Model Is Trackmachine. The Bike Is Manufactured By Bmc. Its Color Is Blue. This racer bike has better lift/drag ratio for improved aerodynamic efficiency. This bike has run 0 kilometers on the road. I cannot increment the odometer. This racer bike has three textile layered performance tires. >>> |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling