1. Static property
Function: Change the way the method in a class is called, without parentheses, as if the data property is being invoked the same way
classFangjian (): Tag='888' def __init__(self,name,owner,wedth,length): Self.name=name Self.owner=owner Self.wedth=wedth self.length=length @propertydefcal (self):returnself.wedth*SELF.LENGTHP1=fangjian ('Yehaibin','Xuzheng', 50,80) A=p1.cal#No brackets required
2. Class Method Classmethod
Functions: Function properties in a class can be called directly using a class, but an instance needs to be created so that instances and classes are bound to a piece, and a way to invoke the methods in the class directly
classFangjian (): Tag='888' def __init__(self,name,owner,wedth,length): Self.name=name Self.owner=owner Self.wedth=wedth self.length=Length@classmethod#It's just for class, not for instance . defClass_method (CLS):Print('This is class ' s method') Fangjian.class_method ()
3. Static method Staticmethod
Role: A class-like toolkit that does not contain classes and instance calls in a method, but only implements a function, such as an output
classFangjian (): Tag='888' def __init__(self,name,owner,wedth,length): Self.name=name Self.owner=owner Self.wedth=wedth self.length=lengt @staticmethoddeftest12 ():Print('This is a test staticmethod') P1=fangjian ('Yehaibin','Xuzheng', 50,80) fangjian.test12 () p1.test12 ()#Both classes and instances can be called directly and use the
4. Combination
The most intuitive feeling is to recognize that the method of the class is actually a dictionary, and then in the dictionary to take the value
#make a course selection system # combination
#目的:
classSchool (): #定义学校类,def __init__(self,name,destin): Self.name=name Self.destin=DestinclassCourse:def __init__(self,name,price,priod,school): Self.name=name Self.prine=Price Self.priod=priod Self.school=school# This is the link between the curriculum and the school school1=school ('Sdau','Shandong') School2=school ('SDAU2','shandong2') School3=school ('Sdau3','Shandong3')#sdau=school (' sdau ', ' Shandong ')#python=course (' python ', ' + ', ' Max ', Sdau.name)#print (python.school) #这是自己输入的值#try to make the user judge and enter informationmsg={ '1': School1,'2': School2,'3': School3} whileTrue:school_chioce=input ('Please Chioce your school' '1:school' '2:schoo2' '3:schoo3') #让学生进行选课, choose your favorite school#Msg[school_chioce] #获得了用户选择的实例school_obj=Msg[school_chioce] #之前已经实例化好了几所学校, after students choose the school, that is, select the other information after the instantiation of the school, you can invoke the method of the instance Course_name=input ('Please input your name') Price= Input ('Please input your price') #根据学生的选择来实例化课程 Priod= Input ('Please input your priod') Final_course=Course (Course_name,price,priod,msg[school_chioce])Print('This is course%s'%Final_course.school.name,)
ifschool_chioce=='Q': Break
5. Combination Test Part1
ImportPickleclassSchool:def __init__(Self,name,addr,banji): Self.name=name Self.addr=addr Self.banji=BanjidefSave (self): with open ('school.db','WB') as F:pickle.dump (self,f)classStudent:def __init__(Self,school,banji): Self.school=School Self.banji=BanjiclassLesson:def __init__(SELF,NAME,PRIOD,PRICE,ADDR): Self.name=name Self.priod=priod Self.price=Price self.addr=AddrclassTeacher:def __init__(self,school): Self.school=School##############School1=school ('Sdau','Beijing',{'Linux':'Class 1','python':'Class 2'})#The school is already there, let the students chooseSchool2=school ('Yingshangyizhong','Shanghai',{'Go':'Class 3'})#Create a courseLesson1=lesson ('python','50week', 150,'Beijing')#courses are available, so students can chooseLesson2=lesson ('Linux','30week', 60,'Beijing') Lesson3=lesson ('Go','100week', 900,'Shanghai')#Student Selectionschool_msg={ '1': School1,#Choose your school '2': School2}lesson_msg={ '1': Lesson1,'2': Lesson2,'3': Lesson3}school_chioce=input ('plesae input your school\n1:sdau\n2:yingshangyizhong\n') School_student=SCHOOL_MSG[SCHOOL_CHIOCE]#determine the selected school based on the values entered by the userifschool_chioce=='1': Course_chioce=input ('Please input your course\n 1:python\n2:linux\n')Else: Course_chioce= Input ('Please input your course\n 3:go') Course_student=LESSON_MSG[COURSE_CHIOCE]#determine the selected course based on the values entered by the user#at this time the school and the curriculum are selected, need to export other information, such as location, classPrint('This is your school information:\nschool:%s\nbanji:%s\ndistination:%s]\n'% (SCHOOL_STUDENT.NAME,SCHOOL_STUDENT.BANJI[COURSE_STUDENT.NAME],COURSE_STUDENT.ADDR))
2018-07-04-python full Stack development day25-static Properties, class methods, static methods, and combinations