1. Inheritance of Classes
Inheritance of Classes
Inheritance is one of the important features of object-oriented,
Inheritance relationship inheritance is a parent-child relationship relative to two classes
Subclasses inherit all the public properties and methods of the parent class.
Inheritance, implementing code reuse
Using inheritance
Inheritance can reuse existing data and behavior, reduce code duplication,
Python uses a pair of parentheses after the class name to represent the inheritance relationship, and the class in parentheses is the parent class
Class Myclass (ParentClass),
If the parent class defines the __init__ method, the child class must explicitly call the __init__ method of the parent class.
Parentclass.__init__ (Self,[args ...])
If the subclass needs to extend the behavior of the parent class, you can add arguments to the __init__ method.
#!/usr/bin/env python#-*-coding:utf-8-*-class People (object): color = ' YELLOW ' def think (self): Self.color = "Black" print "I am a%s"% Self.color print ("I am A Thinker") class Chinese (people): Pass CN = Chine SE () print cn.colorcn.think ()
There are constructors in the parent class:
#!/usr/bin/env python#-*-coding:utf-8-*-class People (object): color = ' YELLOW ' def __init__ (self): print "Init ..." Self.dwell = ' Earth ' def think (self): print "I am a%s"% Self.color print ("I am a th inker ") class Chinese (people): PASSCN = Chinese () Print cn.dwellcn.think ()
Parameters greater than two:
#!/usr/bin/env python#-*-coding:utf-8-*-class People (object): color = ' YELLOW ' def __init__ (self,c): Print "Init ..." Self.dwell = ' Earth ' def think (self): print "I am a%s"% Self.color print ("I am a Thinker ") class Chinese (people): def __init__ (self): people.__init__ (self, ' red ') PASSCN = Chinese ()
Super function
Class A (object): Def __init__ (self): print "Enter a" print "Leave A" class B (object): D EF __init__ (self): print "Enter B" super (B,self), __init__ () print "Leave B" b = B ()
#!/usr/bin/env python#-*-coding:utf-8-*-class People (object): color = ' YELLOW ' def __init__ (self,c): Print "Init ..." Self.dwell = ' Earth ' def think (self): print "I am a%s"% Self.color print ("I am a T Hinker ") class Chinese (people): def __init__ (self): Super (Chinese,self). __init__ (' red ') PASSCN = Chinese () cn . Think ()
#!/usr/bin/env python#-*- coding:utf-8 -*-class people (object): color = ' yellow ' def __init __ (self,c): print "Init ..." self.dwell = ' Earth ' def think (self): print "i am a %s " % self.color print ("I am a thinker") Class chinese (people): def __init__ (self): super (chinese,self). __init__ (' Red ') def talk (self): print "i like taking." Cn = chinese () Cn.think () Cn.talk ()
Multiple inheritance
Python supports multiple inheritance, and the first class can inherit multiple parent classes
Grammar:
Class Class_name (Parent_c1,parent_c2,...)
Attention:
When more than one custom __init__ method appears in the parent class,
Multiple inheritance, only the first tired __init_ method is executed, others are not executed.
#!/usr/bin/env python#-*- coding:utf-8 -*-class people (object): color = ' Yellow ' def __init__ (self): print "Init ..." self.dwell = " Earth ' def think (self): print "i am a %s " % self.color print ("my home is %s ") % self.dwellclass martian (object): color = ' Red ' def __init__ (self): self.dwell = ' Martian ' Class chinese (People,martian): def __init__ (self): people.__init__ (self) cn = chinese () cn.tHink ()
#!/usr/bin/env python#-*- coding:utf-8 -*-class people (object): def __init__ (self): self.dwell = ' Earth ' self.color = ' Yellow ' Def think (self): print "i am a %s " % self.color print (" My home is %s ") % self.dwellclass martian (object): color = ' Red ' def __init__ (self): self.dwell = ' Martian ' def talk (self): print "I like talking" Class chinese (martian,people): def __init__ (self): &Nbsp; people.__init__ (self) Cn = chinese () Cn.think () Cn.talk ( )
3. Summary of the properties of the class
Class property, which is also a public property,
The private property of the class,
The common properties of the object,
object's Private property,
Built-in properties,
Local variables of the function,
Global variables,
#/usr/bin/env python#-*-coding:utf-8-*-class MyClass (object): Var1 = ' class property, public property of Class var1 ' __var2 = ' private property of Class _ _var2 ' Def func1 (self): SELF.VAR3 = ' public property of Object var3 ' SELF.__VAR4 = ' private property of Object __var4 ' VAR5 = ' local variable of function ' MC = MyClass () mc.func1 () #调用后才测打印出var3print mc.var1print mc._myclass__var2print mc.var3mc1 = MyClass () # mc1.func1 () #mc1没有调用方法print MC1.VAR3
access by class:
#/usr/bin/env python # -*- coding:utf-8 -*- # @time :2018/1/2 21:06 # @ author :fengxiaoqing # @file :__init__.py.py # var6 = ' Global variables ' class myclass ( Object): var1 = ' Class property, class's public property var1 ' # #定义在方法外 __var2 = ' class private properties __var2 ' def func1 (self): self.var3 = ' object's public properties var3 ' # #定义在方法内 self.__var4 = ' Private properties of Object  __VAR4 ' var5 = ' Local variables of function ' def func2 (self): print self.var1 print self.__var2 print self.var3 print self.__var4 print self.var6 mc = myclass () mc.func1 () mc.func2 () print ' * ' *50 print mc.__dict__ print myclass.var1 #print MyClass.__var2 #不测通过类访问 print mc.var3 #对象的属性只能通过对象来访问 #print MyClass.__var4 print myclass.__dict__
4. Summary of the methods of the class
Public methods
Private methods
Class method
Static methods
Built-in methods
class myclass (object): name = ' Test ' def func1 (self): print self.name, print "I am the public method."         SELF.__FUNC2 () #func1间接调用了func2的私有方法 def __func2 (self): print self.name, print "I'm a private method." def classfun (self): print self.name, print "I am the class method." def staticfun (self): print s.name, print "I am a static method." Mc = myclass () mc.func1 ()
Calling class methods: using Adorners
@classmethod def classfun (self): print self.name, print "I am the class method." def staticfun (self): print s.name, print "I'm a static method." MC = MyClass () mc.func1 () Myclass.classfun ()
Call a static method:
@staticmethod def staticfun (): Print myclass.name, print "I'm a static method." MC = MyClass () mc.func1 () Myclass.classfun () Myclass.staticfun ()
Call the built-in method:
Class myclass (object): name = ' Test ' def __ Init__ (self): self.func1 ()   SELF.__FUNC2 () self.classfun () self.staticfun () def func1 (self): print self.name, print "I am the public method." def __func2 (self): print self.name, print "I'm a private method." @classmethod def classfun (self): print self.name, print "I am the class method ." @staticmethod def staticfun (): print myclass.name , print "I am a static method." Mc = myclass ()
Inheritance of classes in Python and summary of properties and methods of classes