Python learning notes (15) Python class extension (1) inheritance

inherit

Inheritance is a concept in object-oriented software technology. If a class a "inherits" from another class B, this class A is called "the child class of B", and B is called "the parent class of a", or "B is a superclass of a".

Reuse code

Inheritance of properties and methods

Single inheritance and super function

Example 1: the subclass calls the parent class update web() to execute the parent class update web()

 1 #! /usr/bin/env python
 2 # coding:utf-8
 3 
 4 class Person(object): #New class
 5 
 6     def __init__(self,web_site): #Initialization method
 7         self.web =web_site
 8 
 9     def update_web(self,site):
10         self.web =site
11         return self.web
12 
13 
14 class Cc(Person): #Inherited Person Class, subclass inherits a parent class, called single inheritance
15 
16       def about_me(self,name,site): #Inheritance relation, calling parent class update_web Method
17           my_web =self.update_web(site)
18           return {"name":name,"web":my_web}
19 
20    
21     
22 
23 if __name__ =="__main__":
24     my =Cc("www.weibo.com")
25     print my.about_me("cc","cc.blog.com")
26     print my.web
27 
28 
29 #output
30 #{'web': 'cc.blog.com', 'name': 'cc'}
31 #cc.blog.com

Example 2: subclass overrides the parent class update_web() method, calls in the subclass, executes the update_web() method of the subclass.

 1 #! /usr/bin/env python
 2 # coding:utf-8
 3 
 4 class Person(object): #New class
 5 
 6     def __init__(self,web_site): #Initialization method
 7         self.web =web_site
 8 
 9     def update_web(self,site):
10         self.web =site
11         return self.web
12 
13 
14 class Cc(Person): #Inherited Person Class, subclass inherits a parent class, called single inheritance
15 
16     def update_web(self,site,lang="python"):#The method of the parent class is overridden or overridden here
17          self.web =site
18          self.lang =lang
19          return self.web,self.lang
20 
21    
22     def about_me(self,name,site): #Inheritance relationship, where the method of subclass is called
23        my_web,my_lang=self.update_web(site)
24        return {"name":name,"web":my_web,"lang":my_lang}
25     
26 
27 if __name__ =="__main__":
28     my =Cc("www.weibo.com")
29     print my.about_me("cc","cc.blog.com")
30     
31 #output
32 #{'lang': 'python', 'web': 'cc.blog.com', 'name': 'cc'}

Example 3: call the overridden method in the parent class, use the super() function, or the parent class. Method name

 1 #! /usr/bin/env python
 2 # coding:utf-8
 3 
 4 class Person(object): #New class
 5 
 6     def __init__(self,web_site): #Initialization method
 7         self.web =web_site
 8 
 9     def update_web(self,site):
10         self.web =site
11         return self.web
12 
13 
14 class Cc(Person): #Inherited Person Class, subclass inherits a parent class, called single inheritance
15 
16     def __init__(self,teacher,web_site):
17         self.teacher =teacher
18         #Person.__init__(self,web_site) #Call init method of parent class
19         super(Cc,self).__init__(web_site) #Call the overridden method in the parent class
20         
21     
22 
23     def update_web(self,site,lang="python"):#The method of the parent class is overridden or overridden here
24          self.web =site
25          self.lang =lang
26          return self.web,self.lang
27 
28     def your_teacher(self):
29         return self.teacher
30 
31    
32     def about_me(self,name,site): #Inheritance relationship, where the method of subclass is called
33        my_web,my_lang=self.update_web(site)
34        return {"name":name,"web":my_web,"lang":my_lang}
35     
36 
37 if __name__ =="__main__":
38     my =Cc("cclaoshi","cnblog.com")
39     print my.your_teacher()
40     print my.teacher
41     print my.web
42 
43 #output
44 #cclaoshi
45 #cclaoshi
46 #cnblog.com

Multiple inheritance

Example:

 1 #! /usr/bin/env python
 2 # coding:utf-8
 3 
 4 class Person(object): #New class
 5     def eye(self):
 6         print "two eyes"
 7 
 8     def breast(self,n):
 9         print "The breast is:",n
10 
11 
12 class Girl(object):
13     def __init__(self,age):
14         self.age =age
15 
16     def color(self):
17         print "The girl is white."
18 
19 
20 class BeaGirl(Person,Girl):#Multiple inheritance, write the names of two classes,Inherit all methods of the parent class
21     pass
22 
23 
24 
25 
26 if __name__ =="__main__":
27     kong = BeaGirl(28)
28     kong.eye()
29     kong.breast(90)
30     kong.color()
31     print kong.age
32 
33 
34 #output
35 #two eyes
36 #The breast is: 90
37 #The girl is white.
38 #28

Example: execution order of multiple inheritance, breadth first

 1 #! /usr/bin/env python
 2 # coding:utf-8
 3 
 4 class K1(object): #New class
 5     def foo(self):
 6         print "K1-foo"
 7 
 8 
 9 
10 class K2(object):
11     def foo(self):
12         print "K2-foo"
13 
14     def bar(self):
15         print "K2-bar"
16 
17 
18 class J1(K1,K2):#Multiple inheritance, write the names of two classes,Inherit all methods of the parent class
19     pass
20 
21 
22 class J2(K1,K2):
23     def bar(self):
24         print "J2-bar"
25 
26 class C(J1,J2):
27     pass
28 
29 
30 
31 
32 if __name__ =="__main__":
33     print C.__mro__
34     m =C()
35     m.foo()
36     m.bar()
37 
38 #This order of inheritance is called breadth first
39 #output
40 #(<class '__main__.C'>, <class '__main__.J1'>, <class '__main__.J2'>,
41 #  <class '__main__.K1'>, <class '__main__.K2'>, <type 'object'>)
42 #K1-foo
43 #J2-bar

Keywords: Python

Added by Vorotaev on Sat, 04 Apr 2020 00:43:35 +0300