#!/usr/bin/env python # -*- coding: utf-8 -*- """ @File: inheritance relationship .py _ parent class and subclass @ E-mail: [email protected] @Time: 2020/4/4 12:39 AM @Author:Nobita @Version:1.0 @Desciption: the relationship between parent and child classes / inheritance """ class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.meter = 0 def get_car_description(self): long_name = str(self.make) + ' ' + self.model + ' ' + self.year return long_name DEF read_meter (Self): # read several kilometers method Print ( ' This CAR have have ' + STR (self.meter) + ' mi The ' ) DEF update_meter (Self, mileage): # defines a method to modify several kilometers, several kilometers to be modified after time, there is no need to modify the value of the property by way of example self.meter = mileage DEF increment_meter (Self, Miles): # newly added several kilometers do adder self.meter + = Miles my_car = Car('china', 'audi', '2010') Print (my_car.model) Print (my_car.get_car_description () title ().) # title method syntax -> str.title () // returns "title" of the string, that all words are capitalized to begin. my_car.read_meter () my_car.meter = 50 # by way of example of attribute values do change my_car.read_meter () my_car.update_meter ( 100) # modify the value of the property by the previously defined method my_car.read_meter () my_car.increment_meter(200) my_car.read_meter() class Battery(): def __init__(self, battery_size=70): self.battery_size = battery_size DEF describe_battery (Self): Print ( ' the car ' + STR (self.battery_size) + ' capacitance ' ) def get_range(self): if self.battery_size == 70: car_range = 240 elif self.battery_size <= 100 and self.battery_size >= 75: car_range = 270 MSG = ' the car can run km {} ' .format (car_range) return MSG class ElectricCar (Car): # create ElectricCar (electric vehicle) class DEF __init__ (Self, the make, Model, year): super().__init__(make, model, year) self.battery = Battery() DEF describe_battery (Self): Print ( ' electric vehicle specific attributes} is { ' .format (self.battery.battery_size)) DEF read_meter (Self): # a parent class among, subclass does not help; in subclasses among them the definition of a parent class with the name of the method name, it will ignore the parent class method, its essence. (Method override the parent class) Print ( " This method is a method to rewrite the parent class, ignores read_meter () method of the parent class from among ' ) my_ele_car = ElectricCar ( ' use ' , ' Baoma ' , ' 2020 ' ) print(my_ele_car.get_car_description().title()) my_ele_car.read_meter() my_ele_car.describe_battery() my_ele_car.battery.describe_battery() print(my_ele_car.battery.battery_size) print(my_ele_car.battery.get_range()) my_ele_car.battery.battery_size = 80 # by way of example of attribute values do change Print (my_ele_car.battery.get_range ()) ''' super () is a special function that allows parent and child classes can be associated. This code allows python to call ElectricCar parent class method __init __ (), so that ElectricCar instance contains all the properties of the parent class. Python class initialization method is the __init __ (), so the parent class, subclass initialization method is that, If a subclass does not implement __init __ () function is called to initialize the parent class initialization, If the child class implements this function, this function will have to explicitly call it in the parent class __init __ (), This is with C ++, jAVA do not, they are automatically call the parent class constructor. '''