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


Download 1.25 Mb.
Pdf ko'rish
bet44/48
Sana30.01.2023
Hajmi1.25 Mb.
#1140552
1   ...   40   41   42   43   44   45   46   47   48
Bog'liq
Coding in Python A Comprehensive Beginners Guide to Learn the Realms

Chapter Ten: Importing Classes
You can create a module of Python classes and use it later on to create
different programs. When you save a Python program with the extension .py,
it becomes a Python module. I have saved it with a proper file name. I will
now open a new file, name it as bike.py and save the following code.
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.")

Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   ...   40   41   42   43   44   45   46   47   48




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