17th, 18: Polymorphism of three main characteristics of object-oriented
1 def func (ARG):2 Print (ARG)3 4 #python支持多种形态 (type)5 #优点: You don't need to first declare what character, number type6 #缺点: Look at the source of the time do not know what type7Func1)8Func"HK")9Func ([ One, A, -, -])
Section 19,20,21: Static and Normal fields for class members
1 classfoo:2 #字段 (static field) belongs to class3CC =1234 def __init__ (self):5 #字段 (normal field) belongs to the object6Self.name ='La'7 8 def Show (self):9 print (self.name)Ten Oneobj = foo ()
Example of field location and access rules:
1 classProvince:2Country ="China"#静态字段存在类里, created when code is loaded3 def __init__ (self,name):4Self.name =name #普通字段存在于对象里, when object creation is in memory5 #如果country字段存在对象中被反复调用会占用大量的内存资源6 #省的属性是中国7HN = Province ('Henan')8HB = Province ('Hebei')9SD = Province ('Shandong')Ten print (hn.name) #打印河南的名字 One A - # General: Access your own fields, i.e. class access to static fields, object access to normal fields - #字段访问规则: the # Normal fields can only be accessed with objects - # static fields are accessed with classes (only a last resort to use object access) - print (hn.name) - print (province.country) +#print (Hn.country)
22nd, 23: Common and static methods of class members and classes
Method:
All methods belong to the class
1. Common method: At least one self, object execution
2, static method: Arbitrary parameters, class execution (only as a last resort to use object access)
3. Class method: At least one CLS, class execution (only as a last resort can use object access)
1 classProvince:2Country ="China"3 def __init__ (self,name):4Self.name =name5 6 #普通方法, execution is invoked by the object (method belongs to Class)7 def Show (self):8 print (self.name)9 returnTrueTen One #静态方法属于类, executed by the class to invoke. can have no parameters or can have multiple parameters A @staticmethod - def F1 (arg1,arg2): - print (ARG1,ARG2) the - #类方法由类来调用, is a special form of a static method that has at least one parameter CLS - @classmethod - def f2 (CLS): + print (CLS) - +HN = Province ('Henan') #创建对象 A print (Hn.show ()) #对象调用普通方法 at -PROVINCE.F1 ( A, the) #类调用静态方法 -PROVINCE.F2 () #类调用类方法, print out the name of the class
Section 24,25,26: Attributes of a class member
Python Object-oriented programming (II.)