Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python
Download 1.25 Mb. Pdf ko'rish
|
Coding in Python A Comprehensive Beginners Guide to Learn the Realms
Child Class Attributes
Once the child class inherits the parent class's attributes, you can go on to add to it new methods and attributes. I will use the same program and add new attributes for the child class. 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' def describe_tires(self): print("This racer bike has " + self.performance_tire + " performance tires.") racer1 = RacerBike('URS: Gravel Riding', 'BMC', 'Grey', '2017') print(racer1.fullname()) racer1.describe_tires() racer2 = RacerBike('Trackmachine', 'BMC', 'Blue', '2015') print(racer2.fullname()) racer2.describe_tires() racer3 = RacerBike('Alpenchallenge', 'BMC', 'Red', '2012') print(racer2.fullname()) racer3.describe_tires() = 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. 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. 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. >>> In the next code snippet, I will add another attribute to the child class. This attribute shows why a racer bike has a higher rate of aerodynamic efficiency. 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.") racer1 = RacerBike('URS: Gravel Riding', 'BMC', 'Grey', '2017') print(racer1.fullname()) racer1.describe_tires() racer1.describe_aerodynamics() racer2 = RacerBike('Trackmachine', 'BMC', 'Blue', '2015') print(racer2.fullname()) racer2.describe_tires() racer2.describe_aerodynamics() racer3 = RacerBike('Alpenchallenge', 'BMC', 'Red', '2012') print(racer2.fullname()) racer3.describe_tires() racer3.describe_aerodynamics() = 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. 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. 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. >>> Download 1.25 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling