The first chapter, object-oriented extended content 1.1 abstract interface
Description: Create a base class, that is, abstract interface is to implement a program of the shelf, complete a series of functions, and then the specific content to let the subclass to complete, if not complete, then the call will be directly error.
Role: Generally used to build some shelves of the program, and then the development of multi-person collaboration.
Practice:
Python2
#/usr/bin/env python#-*-encodeing:utf-8-*-ImportAbc#module must fall intoclassAlert (object):#Create a base class __metaclass__= ABC. Abcmeta#Grammar@abc. Abstractmethod#decorate the following method as an abstract method defSend (self):#need to implement methods Pass classMailalert (Alert):#subclass inherits base class Pass #If you do not implementm = Mailalert ()#an exception is reported in the Python2 in the instantiationm.send error message:Traceback (most recent called last): File"Abstract class _python2.py", Line 12,inch<module>m=Mailalert () Typeerror:can'T instantiate abstract class Mailalert with abstract methods send
Python3
#/usr/bin/env Python3classAlert (object):defSend (self):RaiseNotimplementederror#Add an error message to this send method. classMailalert (Alert):Passm=Mailalert () m.send () error message:Traceback (most recent): File"/users/chenxin/s15/day8/abstract class _python3.py", Line 12,inch<module>m.send () File"/users/chenxin/s15/day8/abstract class _python3.py", line 6,inchSendRaiseNotimplementederror#Add an error message to this send method. Notimplementederror
1.2 Static methods
Description: Static methods can be called without instantiation, directly with the class name like the call variable, static method is already with the class has nothing to do with, the only relationship is to use the class name to call it.
Role: Each time the instantiation will open up a piece of memory space, there is memory overhead, if there are tens of thousands of instances, it will cost a lot of instances, it is necessary to think about this static method, the remaining memory space.
How to use: @staticmethod the adorner on a method in a class.
Features: Static methods can neither access public properties nor access instance properties.
Instance
classcc (object):def __init__(self,name): Self.name=name @staticmethoddefect (name,food):Print('%s is eating. %s.'%(Name,food)) p= CC ('chenxin') P.ect ('chenxin','Chicken Leg') Results: Chenxin isEating. Chicken legs.Class 1.3 Methods
Description: In the class method, it does not know the existence of the instance
Role
How to use: @classmethod the adorner on a method in a class.
Feature: Only public properties of the class can be accessed and instance properties cannot be accessed
Instance
class cc (object): Name = " jingjing " def __init__ (Self,name): Self.name = name @classmethod def Span style= "COLOR: #000000" > Walk (self): print ( %s is walking/.... "%self.name) p = CC ( " chenxin " ) P.walk () Result: jingjing is walking/....
1.4 Property Methods
Description
Function: To change a method into a static property, is to become a variable, at this time giving the perception is a variable of access, in fact, access is a method.
How to use: @propery the adorner on a method in a class, if the modified method is to create a method of the same name, on the Decorate @ property method. settr, remove the @ attribute method. deleter
Features: This means that if the parentheses are used, the following method is called, if the property method is called without parentheses.
Instance
classcc (object):def __init__(self,name): Self.name=name @property#the function of a property method is to turn a method into a static property, which is to become a variable. At this point you are accessing a variable, in fact access is a method. defTalk (self):Print('%s says'%(Self.name)) @talk. SetterdefTalk (self,msg):Print('Set msg:', msg) @talk. deleterdefTalk (self):Print('deleter Talk') Call: P= CC ('chenxin') P.talkp.talk='JJJ'delp.talk Result: Chenxin says set MSG:JJJ deleter Talk
Chapter II, Object-oriented more methods 1.1 __doc__
Description: Displays descriptive information for the method
Instance
def Testfun (): """ This function does nothing and just demostrate the use of the doc string. """ Pass Print (Testfun. __doc__)
1.2 __module__
Description: Display the module as a string in combination with import
1.3 __import__
Description: Importing a module as a string
1.4 __call__
Describe:
1.5 __dict__
Description: View all members of a class or object
1.6 __metaclass__
Description: A process that customizes the instantiation of an entire class.
1.7 Type ()
Description: Creates a class dynamically, and all classes are created with the type bottom.
Python (eight)-Object programming extension