‘‘‘
1. Object-oriented concept
2. Type definition
2.0. Inheritance (polymorphism)
2.1. Class attribute (class variable) definition
2.2. Instance attribute (instance variable) definition
2.3. Definition and function of construction method
2.4, the definition of the instance method "Parameters"
2.5. Privatization attribute "principle of realization"
2.6. __slots__ Attribute Declaration restrictions
2.7. Rewriting of the proprietary method of the class (custom Class) "Init, str, Len, Next, enter, exit, Del"
3. Type instance (Production object)
3.1. (package)
3.2. Information acquisition of objects (dir, hasattr, GetAttr, SetAttr, type, isinatance) "polymorphic"
3.3. * Object Properties Use (add, delete, modify, query)
3.4. * The difference and relation between instance attribute and Class attribute
3.5. Invocation of the instance method
3.6. Use of class attributes
‘‘‘
#例子一
classCat (object):" "This is a cat type declaration contains attributes (NAME,AGE,COLOR,PZ) have methods (move, eat, Speak)" " #__slots__= (' name ', ' age ', ' color ', ' PZ ') def __init__(SELF,NAME,AGE,COLOR,PZ): Self.name,self.age,self.color,self.pz=Name,age,color,pzdefMove (self,*node): forXinchnode:Print('%s Location'%x, end="') Print() defEat (Self,food):Print('I'm eating%s .'%Food )defspeak (Self,number):Print('Meow'*Number ) a=cat ('Floral', 8,'Flowers','Dragon Cat') A.name='Small Flower'Print(A.name) dir (a)Print(Type (a))Print(Isinstance (A,cat)) A.move ('Bell tower','Xiao Zhai','High','Changan') A.eat ('Fish') A.speak (5)
#例子2向量类型的简单声明
classV (object):def __init__(self,*p): Self.value=[] forXinchP:ifType (x) ==type (1)orType (x) ==type (0.1): Self.value.append (x)elifx.isdigit (): self.value.append (int (x))def __add__(Self,other): a=Len (self.value) b=Len (other.value) L=eval (str (other.value))#Eval is to take out the contents of a stringmix=Self.valueifA>b:a=b L=eval (str (self.value)) Mix=Other.value forIinchRange (a): L[i]=l[i]+Mix[i]#l=v (*l) #实例化, returnLdef __str__(self):returnStr (self.value) a=v (1,2,3,4,5,6,7) b=v (A) C=a+bPrint(c)Print(C.value)
Python_ Object-oriented