Three main characteristics of object-oriented: inheriting polymorphic encapsulation
Initial knowledge Inheritance:
Subclasses and subclasses instantiate objects that can call any method and variable of the parent class
The class name can access all the contents of the parent class
Objects instantiated by subclasses can also access all the contents of the parent class
Only the methods in the parent class are executed: Do not define the same method as the parent class in the child class
Methods that execute only subclasses: Create this method in a subclass
To execute a method in a child class and a parent class:
1. Passing arguments to a constructor in the parent class
2. Using Super
Class Animal: def __init__ (self,name,age,sex): self.name=name self.age=age self.sex=sex def Eat (self): print ("%s Meat"%self.name) class Cat (Animal): def __init__ (self,name,sex,age,color): # Animal. __init__ (Self,name,sex,age) super (). __init__ (name,age,sex) self.color=color def Eat (self): Print ("%s%s catch Mouse"% (self.color,self.name)) super (). Eat () C=cat ("Jen", ' Male ', 3, ' White ') c.eat ()
The advanced order of inheritance
Inheritance: Single Inheritance multiple inheritance
Class: Classic Class New Class
All classes in Python3 are new classes, which inherit the object class by default
Classic class: Classes that do not inherit object by default are classic classes (both classic and modern in Python2) all classes do not inherit the object class by default
Single inheritance: New class Classic class query Order consistent
Multiple inheritance: New class: Follow breadth First
Classic class: Follow depth First
Breadth First: A road to the penultimate level, judge, if the other road can go to the end, then go back to the other road, if not, go to the end
Depth first: One way to go to the end
18th Day of the Python learning Journey