Python Object-oriented programming
You can have the following data types in a class object
1. Static variables
2. Dynamic variables
3. Private variables
4. Static methods
5. Dynamic Methods
6. Private methods
7. Class methods
8. Features
9. Proprietary methods
First define a class:
#!/usr/bin/env python #coding: Utf-8class Province (object): #静态字段 desc = ' Chinese level ' #动态字段 def __init__ (self, Name,capital,leader,flag=true): #定义专有方法__func__ #动态字段, only instantiated objects can access dynamic fields, and the class itself cannot access dynamic fields self. Name = Name Self. Capital = Capital Self. Leader = Leader self.__pollution = Flag #私有变量定义__var self. dict={' name ': ' Lee ', ' age ': #动态方法, or Object method, only the instantiated object can access the dynamic method Def frankdynamic (self): print ' This is dynamic method ' #静态方法不能访问类变量和实例变量, which is equivalent to a global function #不需要定义实例即可使用这个方法. In addition, multiple instances share this static method and only need to allocate a memory @staticmethod def leestatic (): print ' This is static method ' #特性, access method and field access way @property def allenproperty (self): return to self. Name #类方法 a method that a class method can invoke through an instance of a class object or class object, #不管你是用类对象还是类实例调用这个方法 that the first parameter of the method always defines the class object of the method @classmethod def Marlon Class (CLS): print ' This is class method ' #私有函数, can only be used in the class's dynamic function, or can be called through the instance Object._province__sha, but do not use Def __sha (self) : print ' This is Private function ' Def AswilldynaMic (self): #可以通过动态方法调用私有函数 Self.__sha () @property #通过特性来访问动态私有字段, you can use this adorner, also called the feature Def Pollut Ion (self): print self.__pollution @Pollution. Setter #通过特性, modify dynamic private fields, you can use this adorner, you must combine with the upper function Def pollu tion (self,value): Self.__pollution = value #常用的专有方法 def __call__ (self): #__call__专有方法 print "The is C The All method "return" is the __str__ proprietary method "Def __str__ (self): return" The "This is __str__ proprietary method" Def __geti Tem__ (Self,key): return self. Dict[key] def __setitem__ (self,key,value): self. Dict[key]=value def __delitem__ (self,key): Del self. Dict[key] ################### #以上是类的定义 ##############################
This class is then instantiated, and the use of various data types
#实例化一个对象 heibeiprovince=province (' Hebei ', ' Shijiazhuang ', ' Li Yang ', flag=false) #对象可以访问静态字段print Heibeiprovince.descheibeiprovin Ce. Frankdynamic () #类不能访问动态方法 #province.frankdynamic () #对象和类都可以访问静态方法LeeStaticProvince. Leestatic () Heibeiprovince.leestatic () #特性的访问方式print heibeiprovince.allenproperty #特性, you can access the attribute as you would an Access variable # How to access the Private Function heibeiprovince.aswilldynamic () #该公有函数去调用对象的私有函数__sha () # Modifying a private variable heibeiprovince.pollutionheibeiprovince.pollution= with feature access FalseHeiBeiProvince.PollutionHeiBeiProvince.MarlonClass () #对象可以通过以下方法访问类的私有函数HeiBeiProvince. _province__sha () # Private functions, which can be accessed in this way, but are not used in the application of the # proprietary method #__call__, can use the object of the class as a function using the heibeiprovince () #把类的对象当作函数使用, equivalent to performing heibeiprovice.__ call__ () #__str__print heibeiprovince #会调用专有方法__str__ #__getitem__print heibeiprovince[' name '] #__setitem__print ' Before __setitem__ ', heibeiprovince[' name ']heibeiprovince[' name ']= ' Frank ' print ' after __setitem__ ', heibeiprovince[ ' Name '] #__delitem__del heibeiprovince[' name ']print heibeiprovince.dict
Python Object-oriented programming