Object-oriented:
According to what exists in reality, it simulates its characteristics and is used to describe a particular object.
1. Using functions to accomplish object-oriented
1 defSchool (NAME,ADDR):#School has a name and address2 defdict_ (name,addr):3dict_={4 'name': Name,5 'Addr': Addr,6 'Kao_shi': Kao_shi,#when the corresponding key value is selected, run this function7 'Zhao_sheng': Zhao_sheng8 }9 returndict_Ten defKao_shi (): One Print('Now is Kaoshi') A defZhao_sheng (): - Print('Now is Zhaosheng') - returndict_ (NAME,ADDR) the #Run - -P1=school ('Yehiabin','Shandong') -p2=p1['Kao_shi']()#run from selection of functions + #The various properties of the class, in fact, are in this dictionary, when called, is to take the corresponding key value
This is the use of functions to accomplish object-oriented design, using school to simulate the characteristics of a school, in the characteristics of its data properties and function properties, and then can be called, to school the value of the time, that is, a generation of instances of the process
2. Classes and instances
In Python, class is used to define classes, classes have data properties and function properties, and in order to save memory, the instance has only data attributes and is tuned to the class when a function attribute is required.
2.1 Attribute additions and deletions
2.1.1 View
classChina:#when declaring a class, it is usually preceded by uppercase def __init__(self,age): Self.age=age#Given to self an age method, the value of the input age,,p.age=ageName='Yehaibin'#Data Properties # For additional deletions and checks defQiong (self):Print('Zhengzaitutan', Self.age)#ViewPrint(China.name)
2.1.2 Modification
classChina:#when declaring a class, it is usually preceded by uppercase def __init__(self,age): Self.age=age#Given to self an age method, the value of the input age,,p.age=ageName='Yehaibin'#Data Properties # For additional deletions and checks defQiong (self):Print('Zhengzaitutan', Self.age) China.name='Heiheihie'
2.1.3 Delete
class china:# When declaring a class, you typically start with uppercase def __init__ (self,age): self.age =age# gives self an age method with a value of input age,,p.age=age name=" yehaibin " # data properties # for def Qiong (self): print ( ' zhengzaitutan " ,self.age ) del china.name
2.1.4 Increase
classChina:#when declaring a class, it is usually preceded by uppercase def __init__(self,age): Self.age=age#Given to self an age method, the value of the input age,,p.age=ageName='Yehaibin'#Data Properties # For additional deletions and checks defQiong (self):Print('Zhengzaitutan', Self.age)defEat (self,name):Print('%s adssa%s'%(Self.age,name)) China.eat=Eatchina.gender='NaNs'
2.2 Classes and examples
1. When a method is called, the instance automatically passes in the self value, and the class does not
2. Any use. To tune the method, you must follow the method of the class, that is, you cannot reference it from a global variable
Name='123'class Test: name='567' def Test (): print(name) P1=test.testPrint (p1)---123# because name is used directly, the global variable is called,
Ame='123'class Test: name='567' def Test (self): print(self.name) P1=Test () Print(P1.test ()) #此时调用了类的方法, which is the effect of scopes within a class
3. The scope of a function can be seen in a class
4.
2018-07-03-python full Stack Development day24-object-oriented design