---inheritance
When we define the completion of a class, we can define a new class, and the new class can inherit the first class. The new class is called a subclass, and the inherited class is called the parent class/base class/superclass.
Inheritance is the child class that inherits the properties and methods of the parent class (Note the class properties and class methods).
Inheritance enables subclasses to use methods from the parent class, or to define a new method in a subclass, or to overwrite a parent class's method in a subclass.
Consider an example:
Class Animal (object): #定义一个动物类 def running (self): print (' Running ... ') Class dog (animal): #定义一个狗类, And dogs inherit the animal def Wangwang (self): print (' wang! wang! ') Xiaohei=dog () xiaohei.running () Xiaohei.wangwang () #结果running .... wang! wang!
As we can see, in the above example, after the dog class inherits the animal class, there is no need to redefine the running method, and the running method in animal can be called directly. This is one of the advantages of inheritance.
What happens if we define a running method in the dog class?
Class Animal (object): def running (self): print (' Running ... ') Class dog (animal): def Wangwang (self): print (' wang! wang! ') def running (self): print (' dog was running ') #结果dog is running #父类的方法被子类同名的方法覆盖wang! wang!
That is, a method in a subclass overrides a method of the same name in the parent class!
How does a subclass execute the constructor of a parent class?
1 Super (Son, self). __init ()
2 father.__init__ (self)
View Code
---multiple inheritance
Multi-inheritance means that a subclass can inherit multiple parent classes. You may be asked:
1 How can I achieve multiple inheritance?
2 if each parent class has a method with the same name, which is the final call when the child class calls the method?
1 First of all, how can we achieve multiple inheritance?
Class Animal (object): def running (self): print (' Running ... ') class mammal (object): def Grow (self): print (' suckling ') class Dog (Animal,mammal): #<-----------include the parent class name in parentheses, separated by a comma, to def Wangwang (self): Print (' wang! wang! ') def running (self): print (' dog is Running ') Xiaohei=dog () xiaohei.running () Xiaohei.wangwang () Xiaohei.grow () # Results Dog is runningwang! wang! Suckling
Note: Python classes can inherit multiple classes, and Java and C # can inherit only one class.
2 second question: If each parent class has a method with the same name, which is the final call when the child class calls the method? Or what is the order of succession?
Before answering this question, we need to understand the classic class and the new class first.
What is a classic class and what is a new class?
Class class_name: #经典类的定义方法 passclass class_name (object): #新式类的定义方法 Pass
New classes Add a lot more functionality than classic classes, so modern classes are now recommended.
In Python3, either the new class or the classic class inheritance order is depth-first
Class A (object):
def f (self):
Print (' a ')
SELF.F1 ()
def F1 (self):
Print (' A-f1 ')
Class B (Object):
def F1 (self):
Print (' B ')
Class C (a):
def F1 (self):
Print (' C ')
Class D (b):
def f (self):
Print (' d ')
Class E (c,d):
def F1 (self):
Print (' E ')
e1 = e ()
E1.F ()
But there is a special case:
If both A and B are subclasses of the all class, you will not find all after you find a, but find D.
We can summarize the order of precedence of Python in multiple inheritance:
1 itself the largest, first find from itself
2 left-to-right priority descending, depth first
3 If there is a common parent class between the left parent class and the upper layer of the right parent class, move the common parent class and its upper layer to the depth level of the right parent class
---method
Methods in Python include common method/static method/class method. However, static methods and class methods are not commonly used.
All methods belong to the class attribute, that is, there is only one copy in memory
Common method: Can only be called by an instance, with at least one self argument
Static method: Can be called without instantiation, no default parameters
Class method: Can be called without instantiation. There is at least one CLS parameter
View Code
---properties
We have previously said that by adding two underscores to a variable in a class, you can "hide" the variable so that it is not visible externally.
Class Atm00 (object): def __init__ (Self,money): Self.money=money def Show_money (self): print (' The ATM has%s¥ '%self.money) a=atm00 (+) A.show_money () A.money=120a.show_money () #结果the ATM has 100¥the ATM have 120¥ #很明显 we can To assign values directly externally to the A.money, which is simple, but we cannot verify that the money entered by the user is legal
At this point, we can modify this:
Class Atm01 (object): def __init__ (Self,money): Self.__money=money def Show_money (self): print (' The ATM has%s¥ '%self.__money) def Set_money (Self,money): #当修改money时, you can also detect if the value entered by the user is legal if not isinstance ( Money,int): print ("Money ' s value must is Interger") else: self.__money=moneya=atm01 (+) A.show_money ()
A.set_money (120
By modifying it, we make the code more robust and more secure, but without the convenience of having previously assigned values directly externally. Is there a way to directly assign values externally and have the legitimacy to detect user input values? We're going to use the property here!
Class Atm01 (object): def __init__ (self,money_value): self.__money=money_value @property #第一种属性 def money: return Self.__money @money. Setter #第二种属性 def money (self,money_value): i F not Isinstance (Money_value,int): print ("Money ' s value must is Interger") else: self.__money=money_ Value @money. deleter #第三种属性 def money (self): print (' no money ') a=atm01 (A.money) A.mone Y=120print (A.money) del a.money# result 100120no money
The above code may not be able to understand, it's okay, just to illustrate the importance of attributes, then we will formally explain the properties.
How do I define a property?
There are three types of properties.
First look at the first attribute:
Class Test (object): def step1 (self): pass @property @property Adorner, the method becomes the attribute def step2 ( Self): return "Hello World" a=test () print (A.STEP2) #结果hello World
Note two points:
1 only one self parameter can be included in a property
2 No parentheses are required when calling properties
Second and third properties:
# ############### definition ############## #class Goods (object): @property def Price: print ' @property ' @price. Setter def price (self, value): " print ' @price. Setter ' @price. Deleter def Price ( Self): print ' @price. Deleter ' # ############### call ############## #obj = Goods () obj.price # performs @property-decorated The price method and gets the return value of the method Obj.price = 123 # Executes the @price.setter decorated price method and assigns 123 to the method's parameter del obj.price # The price method for performing @price.deleter adornments
To summarize, these three attributes are displayed, modified, and deleted.
Object-oriented 02 of the Python Foundation