Object-oriented design is introduced into object-oriented, and finally object-oriented programming.
#!/usr/bin/env python#Coding:utf-8defSchool (Name,addr,type):defInit (name, addr, type): Sch= { 'name': Name,'Addr': Addr,'type': Type,'Kao_shi': Kao_shi,'Zhao_sheng': Zhao_sheng,}returnSchdefKao_shi (school):Print('%s School is taking an exam'%school['name']) defZhao_sheng (school):Print('%s%s is enrolling'% (school['type'],school['name'])) returnInit (name,addr,type)Print("Object-oriented design") S1=school ('Oldboy','Shahe','Private Schools')Print(S1)Print(s1['name']) s1['Zhao_sheng'] (s1) S2=school ('Tsinghua','Beijing','Public Schools')Print(S2)Print(s2['name'],s2['Addr'],s2['type']) s2['Zhao_sheng'] (S2)
Classes of Exercises:
#!/usr/bin/env python#Coding:utf-8## class: Combining the same characteristics and actions of a class of things together is a kind of abstract concept## object: A specific thing that is created based on a class (concrete) is a combination of features and actions#class Chinese: # Distinction in classic class Python2#" This is a Chinese class "#Pass##Print (Chinese)###class Chinese (object): # The distinction in the new class Python2#Pass#what exactly did the instantiation do? #p1 = Chinese () # Instantiation does not use new as in other languages#print (p1)## No more distinctions in python3, all new typesclassChinese:" "Write a document note here" " #1 Data PropertiesArea ="Asia" #2 Function Properties defDun ():Print("Asian Squat") defSpeak (self):Print("speak Chinese%s"%Self )Print(dir (Chinese))Print(Chinese.__dict__)#view the class's property dictionary, which holds the class's data properties and function PropertiesPrint(Chinese.area) Chinese.dun () Chinese.__dict__['Dun'] () Chinese.__dict__['speak']("Hello")Print(Chinese.__name__)#class namePrint(Chinese.__doc__)#document comments for classPrint(Chinese.__base__)#base class <class ' object ' >Print(Chinese.__bases__)#base class (<class ' object ' >)Print(Chinese.__module__)#from the module
Class of Exercise II:
#!/usr/bin/env python#Coding:utf-8classChinese:" "Write a document note here" " Area="Asia" #def init (name,age,gender): #dic={ #' name ': Name, #' age ': Age, #' gender ': gender # } #return dic def __init__(self, name, age, gender):Print("start ...") Self.aname=name Self.aage=Age Self.agender=GenderPrint("End ...") ## Auto Return None So, can't add return defdun (self):Print("%s Asian squatting in%s"%(Self.aname,self.area))defspeak (Self,lang):Print("%s Speaking%s"%(self.aname, Lang)) P1= Chinese ("Tom", 22,"male")#instantiation ofPrint(P1.__dict__)Print(P1.aname)Print(P1.agender)Print(P1.area) P1.dun () P1.speak ('Local dialect')Print(P1.area)#ModifyChinese.area ='Eastern Continent'#Modifying FunctionsdefXia_dun (self):Print("%s sat on the ground. "%self.aname) Chinese.dun= Xia_dun#the inner function of the class has been modified#IncreaseChinese.skin ='Yellow'P2= Chinese ("Xiao Ming", 11,"male")Print(P2.area)Print(P2.skin)Print(P1.skin)#the previous instance can also be used, because the instance is a reference to the classP2.dun ()#Delete#del Chinese.area#print (P2.area)
Teacher Http://www.cnblogs.com/linhaifeng/articles/6182264.html
Python Object-oriented