1. Polymorphism (multiple types)
The manifestation of polymorphism is as follows:
def func (ARG): Print (ARG) func (1) func ("Alex") func ([ 11,12,13] #func can be anything # but not for C#,java, arguments must specify type
The disadvantage of polymorphism: In the absence of annotations, it is not known what type of ARG is, what is the use of it. For example, if it is OK to be a list, then you can append (). But I don't know how to use it when I'm not sure.
2. Class members in Object-oriented
1) field
-static field: The code was created when it was loaded and belongs to the class
-Normal field: There are objects to this, belong to the object
class Province: " China " # static fields, saved in class def __init__ (self,name): # stored in the object, this is a normal field, if this function is not called, then this is not in memory
2) method (belongs to Class)
-Common method: definition must have self, execute through object
-static method: @staticmethod, remove self, parameter is optional, executed by class
-Class method: @classmethod, a special form of a static method. Automatic class parameters, executed by class
classProvince:def __init__(self,name): Self.name=namedefShow (self):#Common Methods Print(self.name) @staticmethod#static methods, 1. Add this, 2. Remove self,3. Parameters are optional defF1 (arg1):Print(arg1) @classmethod#class method, you must have parameters defF2 (CLS):#This parameter is passed automatically and is the class name. One of the static methods. Print(CLS)defF3 (self):returnSelf.name[1]##方法是由对象调用的#obj = Province ("Henan")#obj.show ()##普通方法由对象去调用执行 (method belongs to Class)#PROVINCE.F1 (111) #静态方法, is only related to the class, does not need the object, is called by the class, the same as the function#province.f2 () #类方法, parameter is class name#obj = Province ("Alex")#ret = obj.f3 ()#print (ret)
The way of the Python-day 8-polymorphic, class member, singleton mode, exception handling