Small summary:
Object-oriented Benefits
Easier to scale, improve code efficiency, make code more organized, clearer, and more suitable for complex project development
Encapsulation: encapsulates the implementation details of the function, exposing the calling Interface.
Polymorphism: Inheritance of interfaces
Defined:
Class: templates
Objects: instantiated Classes
Properties: Private Property __private
The public attribute exists in a variable in the class
Member Property Instance variable
method: function
Constructor function:
Destructor: The instance is destroyed (delete the variable manually or the program ends), call the destructor
New Class vs Classic class:
New Class: inheritance Path: breadth First
Classic Class: Inheritance Path: depth First
one, Abstract Interface:
1 classAlert (object):2 " "Alarm Class" "3 defSend (self):4 RaiseNotimplementederror5 classMailalert (Alert):6 defSend (self,msg):7 Print('_____sending:', Msg)8 classsmsalert:9 PassTenm=Mailalert () oneM.send ('ooooooooooooooooo')
second, class Method:
Class methods can access only the public properties of a class and cannot access member properties and private properties
1 classperson (object):2Id='11111111111111111'3 def __init__(self,name):4Self.name=name5 @staticmethod6 defEat (name):7 Print('%s is eating ...'%Name)8 @classmethod9 defh (self):Ten Print('%s'%(SELF.ID)) one #do not need to instantiate directly through the class call aPerson.eat ('eeeeeeeeeee') -P=person ('Song')#class methods can only invoke public properties, not um, call member properties and private properties -P.h ()
Static methods can neither access public properties nor access instance properties
third, Attribute method
Turn a method into a variable
1 classperson (object):2Id='11111111111111111'3 def __init__(self,name):4Self.name=name5 @property6 deftalk (self):7 Print('%s say'%Self.name)8P=person ('Songxiaonan')9 #p.talk () #报错TenP.talk#Songxiaonan say
After the attribute method becomes a variable, attach a value @talk to the Variable. setter
1 classperson (object):2Id='11111111111111111'3 def __init__(self,name):4Self.name=name5 @property6 deftalk (self):7 Print('%s say'%Self.name)8 @talk. Setter9 deftalk (self,msg):Ten Print('talk %s'%Msg) oneP=person ('Songxiaonan') a #p.talk () #报错 -P.talk#Songxiaonan say P.talk call is the first talk -p.talk='Ahahhahhahahaha' #talk Ahahhahhahahaha p.talk= value is called the second talk
Delete the variable @talk. deleter
1 classperson (object):2Id='11111111111111111'3 def __init__(self,name):4Self.name=name5 @property6 deftalk (self):7 Print('%s say'%Self.name)8 @talk. Setter9 deftalk (self,msg):Ten Print('talk %s'%Msg) one @talk. deleter a deftalk (self,msg): - Print('Delete ... ..')
Del P.talk
Iv. other special methods of class
1 classAnimal (object):2country='Beijing'3 def __init__(self,name,age):4Self.name=name5Self.age= age6 def __str__(self):#return results for custom instances7 return '%s--%s'%(self.name,self.age)8 9A = Animal ('CAT',' a')Ten PrintA.__dict__)#Print Instance's Properties {' name ': ' CAT ', ' age ': ' + '} one PrintA.__module__)#the module where the print instance resides __main__ a PrintA.__class__)#the class <class ' __main__ where the print instance Resides. Animal ' > - PrintA#cat--12
Python-object-oriented Advanced