1. Object-Oriented Programming
Object-oriented design by integrating data and actions of a specific kind of thing
1.
Sch = { ' name ': ' Tsinghua University ', ' TP ': ' Public school ', ' addr ': ' Beijing '}def Shangke (x): print ("%s is in class"% x[' name ']) def Zhaosheng (y): print ("%s%s is enrolling"% (y[' TP '], y[' name ')) Shangke (Sch) Zhaosheng (Sch) Tsinghua University is attending a public school, Tsinghua University is enrolling
2.
Def school (NAME,TP,ADDR): def shangke (x): print ("%s is in class"% x[' name ']) def zhaosheng (y): print ("%s%s Enrolling "% (y[' TP '), y[' name '])) sch = { ' name ': Name, ' TP ': TP, ' addr ': addr, ' Shangke ': Shangke, ' Zhaosheng ': Zhaosheng } return SCHS1 = School (' Peking University ', ' Public School ', ' Beijing ') s1[' Shangke '] (S1) s1[' Zhaosheng '] (S1) Peking University is attending public school Beijing University is enrolling
3.
Def school (NAME,TP,ADDR): def init (name,tp,addr): sch = { ' name ': Name, ' TP ': TP, ' addr ': Addr, ' Shangke ': Shangke, ' Zhaosheng ': Zhaosheng } return sch def shangke (x): print ( "%s is in class"% x[' name ']) def zhaosheng (y): print ("%s%s is enrolling"% (y[' TP '], y[' name ')) return init (name, TP, a DDR) S1 = School (' Peking University ', ' Public School ', ' Beijing ') s1[' Shangke '] (S1) s1[' Zhaosheng '] (S1) Peking University is attending public school Beijing University is enrolling
2. Object-Oriented Programming
Implement object-oriented design by defining class + instance/object
1.
The following class is called the Classic class in Python2 (Python3 is called the Modern class people (object):p):
class people: li = ' Lao li ' def chuan_men (): print (' Go next door ') A = People () # class's instantiation of print (people) print ( People.li) people.chuan_men () print (a) <class ' __main__. People ' > Lao Li went to <__main__ next door. People object at 0x7f7756c009e8>
2.
The process of instantiating is essentially invoking the run of the __init__ (self) function, initializing the constructor.
Class people: def __init__ (self,name,age,gender): print (' init start ') Self.mingzi = name self.nianling = Age Self.xingbie = gender print (' init end ') p = people (' Next door Old king ', ' 45 ', ' Man ') # Instantiation of print (p.__dict__) Init starts init end {' nianling ': ' Xingbie ', ' The ' Man ', ' Mingzi ': ' The old king next Door '}
3.
There is no Li in the property Dictionary of P, it will go to the property Dictionary of the class penple. There is no function attribute in the property Dictionary of P, and calling Motou (self) actually calls the Motou (self) function inside the class people. Self is the instance itself.
class people: li = ' Lao li ' def __init__ (self,name,age,gender): Self.mingzi = name self.nianling = Age Self.xingbie = Gender def Motou (self): print ('%s touch your head '% Self.mingzi) p = people (' Old king next Door ', ' 45 ', ' Man ') # instanced print (p.__dict__) print (p.li) P.motou () People.motou (p) {' Xingbie ': ' Male ', ' mingzi ': ' Old king next Door ', ' nianling ': ' 45 '} Lao Li's next door touched your head, and the old king touched your head.
Instances can access class properties (scopes), and classes cannot access function properties. The instance has only data properties, and the method (function attribute) goes to the class to find (Reference).
-----
Python Object-oriented