facing Objects1. Encapsulation#What is the encapsulation in Python? #use the construction method to encapsulate the content into an object, and then indirectly obtain the encapsulated content through the object directly or self; classOop (object):def __init__(self): Self.name=name Self.age= Agedefget_attrs (self):Print(Self.name)Print(self.age) obj= Oop ('Mic', 18) #Direct Access Print(Obj.name, Obj.age)#Indirect AcquisitionObj.get_attrs () attached: What is the difference between a functional change and a face-to-object change? #if you want to pass in a fixed parameter to complete a few different things, for the function, each time you have to pass the same parameters, #oop only needs to write different things as a method into a class, and the passed parameters are saved in the created object . #You only need to pass in the arguments when the object is instantiated, and then each method can be indirectly taken with self;2. Inheritance#What is inheritance? #is the method that will be common to multiple classes, in the parent class, the subclass only needs to inherit the parent class, #Each method is not required to be written once in each class; #Multiple Inheritance #the difference between a new class and a classic class:1. Classic ClassclassGrandpa:defSay (self):Print('Grandpa') classFather (Grandpa):defSay (self):Print('Father') classmother (Grandpa):defSay (self):Print('Mother') classChild (Father, mother):defSay (self):Print(' Child') #After creating a son object, execute the Say () method and find the Say () method in the Father class first . #can not find it to the previous parent class grandpa, found not to start from the mother class to find, #No more can not find the error;son =Child () A.say ()2. New Class#The code is basically the same as above, just Grandpa: modified to: ' Grandpa (object): '; #execution Order different:father-> mother-> GRANPA error;3. polymorphic#What is polymorphic? What is polymorphism? #Polymorphism refers to multiple forms of the same thing; #Polymorphism refers to a method of invocation, different execution effect; #The advantage: No matter how the object changes, the user does not need to modify the code, all the same form call execution; classMic (object):defwrite ():Print('Mic') classTom (object):defwrite ():Print('Tom') defExe_func (obj): Obj.write () obj1=Mic () obj2=Tom ()#call the same function, but the execution effect is different, even if the Write method is modified, there is no need to replace the interface;Exe_func (obj1) exe_func (OBJ2)
Package | Inheritance | polymorphic | Python