1. Subclass Method __init__ ()
When a class inherits another class, he automatically obtains all the properties and methods of the other class. The original class is called the parent class, and the new class is called a subclass. A subclass inherits all the properties and methods of its parent class, and can also define its own properties and methods.
When you create a subclass, the parent class must be contained in the current file and in front of the subclass
When you define a subclass, you must specify the name of the parent class in parentheses.
Class Car (object): "" DocString for Car "" Def __init__ (self, make, model, year): Self.make = Make Self.model = mode L Self.year = Year self.reading = 0 def get_name (self): long_name = str (self.year) + ' + self.make + ' + self. Model return Long_name.title () def read_odometer (self): print (str (self.reading)) def updata_odometer (self, mileage ): self.reading = Mileage def increment_odometer (self, Miles): self.reading + = miles My_car = car (' Audi ', ' A4 ', 2 016) Print (My_car.get_name ()) my_car.reading = My_car.read_odometer () my_car.updata_odometer (My_car.read_) Odometer () My_car.increment_odometer () My_car.read_odometer () class Electriccar (CAR): "", DocString for Electriccar "" Def __init__ (self, make, model, year): ' Initialize the properties of the parent class ' super (Electriccar, self). __init__ (make, model, year) #self . arg = arg My_tesla = Electriccar (' Tesla ', ' Model S ', 2016) print (My_tesla.get_name ())Result :2016 Tesla Model S
For the inheritance in Python 2.7, the function super () requires two arguments: the subclass name and the object self. I am currently using the compiler for sublime Text 3, plus both parameters can be run correctly.
2. Define properties and methods for a given subclass
After a subclass inherits another class, you can add new properties and methods required by the zone's molecular class and the parent class
Class Electriccar (CAR): "" "
docstring for Electriccar" ""
def __init__ (self, make, model, year): ""
Initializes the parent class's properties '
super (). __INIT__ (make, model, year)
#self. arg = arg
self.battery =
def describe_ Battery (self):
print ("This car has a" + str (self.battery) + "-kwh Battery")
My_tesla = Electriccar (' Tesla ', ' mode L s ', 2016)
my_tesla.describe_battery ()
print (My_tesla.get_name ()) Result: The car
has a 40-kwh Battery
11
3. Overriding the parent class method
For a method of a parent class, it can be overridden as long as it does not conform to the behavior of the object modeled by the subclass. The overridden method is to have the same name as the parent class method, and when you call the method, Python only cares about the corresponding method defined in the subclass.
4. Use an instance as a property
Class Electriccar (CAR): "" "
docstring for Electriccar" ""
def __init__ (self, make, model, year): ""
Initializes the parent class's properties '
super (). __INIT__ (make, model, year)
#self. arg = arg
self.battery = Battery ()
#def Describe_battery (self):
#print ("This car has a" + str (self.battery) + "-kwh Battery")
My_tesla = Electriccar (' Te SLA ', ' Model S ', 2016)
my_tesla.battery.describe_battery ()
print (My_tesla.get_name ()) result
: This car
has a 40-kwh battery
2016 Tesla Model S