Several methods of introducing class - Class - example import module

car.py

class Car ():
     '' ' a simple attempt to simulate cars ' ''

    DEF  __init__ (Self, the make, Model, year):
         '' ' initialization described the car: manufacturer, model, year of manufacture ' ''

        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 24-               # odometer reading

    DEF update_odometer (Self, mileage):       # modify the attribute value of 
        '' the odometer reading to the specified value of '' '

        self.odometer_reading += mileage


    DEF get_descriptive_name (Self):
         '' ' returns clean the description ' ''

        long_name = str(self.year) + '   ' + self.make + '   ' + self.model

        return  long_name.title()

    DEF read_odometer (Self):
         '' ' print information that indicates a mileage of cars ' '' 
        Print ( ' car mileage car is:   ' + str (self.odometer_reading))


    DEF fill_gas_tank (Self):
         '' ' fuel tanks ' ''

        Print ( ' car oil tank ' )

#----------------------------------------------------------------------
#--------------------------------------------------------------------
#------------------------------------------------------------------

class Battery ():
     '' ' analog electric car battery ' ''

    DEF  the __init__ (Self, battery_size):
                '' ' initialization battery properties ' '' 
               self.battery_size = battery_size


    DEF describe_battery (Self):
         '' ' print a description of the battery capacity ' ''

        Print ( ' car electric car: ' + STR (self.battery_size) + " battery capacity " )


    DEF fill_gas_tank (Self):                # override the parent class 
        '' ' electric car battery ' ''

        Print ( ' car has batteries ' )


    def get_rang(self):

        Print ( ' car travel distance is:   ' + STR (self.battery_size *. 3 ))
 # --------------------------- ------------------------------------------- 
# ------ -------------------------------------------------- -------------- 
# ----------------------------------- -------------------------------

class ElectricCar (Car):
     '' ' unique electric vehicles ' ''

    DEF  the __init__ (Self, the make, Model, year):
         '' ' initializes the parent class attributes ' '' 
        Super (). the __init__ (the make, Model, year)

        self.battery = Battery(100)

my_cars.py

from the plurality of class car import Car, ElectricCar # import module
# Import car # import entire module
# From all classes car import * # import module


my_beetle = Car('fengtian','beetle',2011)
print(my_beetle.get_descriptive_name())

# ------------------------------------------------- ------- 

my_tesla = ElectricCar ( ' Tesla ' , ' Model S ' , 2016 )

print(my_tesla.get_descriptive_name())


my_tesla.battery.fill_gas_tank()

my_tesla.battery.describe_battery()

my_tesla.battery.get_rang()

 

Results of the:

 

2011 Fengtian Beetle

 

 


2016 Tesla Model S


There are car batteries


Car electric car: the battery capacity of 100


The car's driving distance: 300

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12061810.html