Content directory:
- Polymorphism of the three characteristics of object-oriented
- Members in Object-oriented: Fields, Methods, properties
- Member modifiers for class
- Special members of the class
- Special Member methods
- Object-oriented Other
- Exception handling
- A singleton pattern of design patterns
Object-oriented polymorphism
Polymorphism: Refers to many types of forms
For example, in a function can define the DEF func (ARG), ARG can be an integer, can be a string can be a dictionary can be a list and so on many types, which is object-oriented polymorphism.
#python中def func (ARG): print (ARG) func (1) func (' Jabe ') func ([11,22,33]) #python parameters can be integers, strings, lists, etc. that are object-oriented polymorphic # C #/java def func1 (int arg): You need to specify the parameter type print (ARG) func1 (123) func1 (' 123 ') #报错class A: passclass B (in #c #/java) when defining parameters A): Passclass C (a): pass def func (a arg): #arg的参数必须是A类型或者A类的子类 Pass
Object-oriented Members
- Fields: include static fields and normal fields
- Methods: including static methods and common methods
- Properties: Static Properties
First, the field
Fields are divided into static fields and ordinary fields, the main difference is that they exist in memory in different locations, static fields exist in the class, the class created after the static field has been generated, and ordinary fields exist in the object, only after the instantiation of the normal fields can be generated in memory, the normal field when creating a class does not occupy memory.
Class Province: #静态字段 country = ' China ' def __init__ (self,name): #普通字段 self.name = name# normal field direct access mode, Accessing ordinary fields by Object obj = Province (' Hebei province ') print (obj.name)
#静态字段访问方式, access print directly from the class call mode (Province.country)
PS: Static fields are generally accessed by classes, and objects can be accessed but not accessed by objects.
A static field saves only one copy of the data in memory, and the normal field is kept in one copy of each object; When you create an object from a class, you use a static field if each object has the same field
Second, the method
Methods are divided into ordinary methods, static methods and class methods, all of the methods belong to the class, in memory is only one copy, the difference is that the calling method is different, the various ways to call the parameters are different.
Common method: At least one self, executed by the object invocation;
static method: Called by the class execution, you can add arbitrary parameters;
Class method: At least one CLS parameter, executed by the class invocation;
Class Province: country = ' China ' #静态字段 def __init__ (self,name): self.name = name #普通字段 print ( Self.name) #普通方法 def Show (self,name): #print (self.name) print (123) #静态方法 @staticmethod def F1 (arg1,arg2): print (ARG1,ARG2) #类方法 @classmethod def f2 (CLS): print (CLS) PROVINCE.F1 (111,222) #静态方法调用Province. F2 () #类方法调用 returns the current class name <class ' __main__. Province ' >obj = Province (' Hebei ') obj.show (' Hebei ') #普通方法调用
Third, attribute
Attributes are special common methods, methods and field combinations, method definitions, and field invocation methods.
How to define:
1 adding @property adorners on the basis of common methods;
2 property has only one self parameter
3 Call without parentheses, method: Obj.func () Property: Obj.func
Basic use
Class Pager: def __init__ (self,all_count): self.all_count = all_count #属性的定义 @property def All_ Pager (self): a1,a2 = Divmod (self.all_count,10) if a2 = = 0: return A1 else: return a1+1 # The setting of the property @all_pager. Setter def all_pager (self,value): print (value) #属性的删除 @all_ Pager.deleter def all_pager (self): print (' del all_pager ') p = pager (101) # P.all_count #字段 # ret = P.all_pager # The call to the property is less than the normal method call bracket # print (ret) P.all_pager = one #属性的setter, can be directly assigned, call @all_pager.setter method del P.all_pager #属性的删除 call @all _pager.deleter method
Another way to define a property
Class Pager: def __init__ (self,all_count): self.all_count = All_count def f1 (self): return 123 def f2 (self,value): pass def f3 (self): pass foo = property (fget=f1,fset=f2,fdel=f3) #直接将f1, F2, F3 into the property specifies the keyword P = Pager (101) Resault = P.foo #自动调用fget的方法print (resault) P.foo = ' Jabe ' #fset F2 method del P.foo #自动调用fdel的方法
Member modifiers for class
Members of a class we can know from above that including fields, methods, and properties, there are two forms for each member of a class:
Public members: accessible anywhere, i.e. both internal and external to the class;
Private Member: Accessible only within the class;
Python operations Development (eight)----object-oriented (bottom)