python foundation - object oriented 2 - encapsulation

Object oriented programming has three characteristics: encapsulation, inheritance and polymorphism
python is also an object-oriented language

To illustrate with an example:

class user(object):

    def __init__(self, name, age,work):
        self.name = name
        self.age = age
        self.work = work

    def __str__(self):      #Concatenate string
        return self.__name + str(self.__age) + str(self._work)

if __name__ == '__main__':
    u = user("Hee hee",19,"doctor")
    print(u)
    print(u.name)       #Direct access to object properties
    u.name = "xixi"     #Can be modified directly
    print(u)     

Operation result:

Through direct access to properties, disadvantages:
(1) Data may be tampered with
(2) Class change is difficult to maintain and error prone
In order to prevent the above situation, we privatize its properties and do not allow them to be directly accessed. The privatized properties and methods can be accessed internally, but they can no longer be accessed outside the class. Only by providing public methods can they be accessed and modified, which is encapsulation.

Package 1
Concept: a class encapsulates properties and methods as a whole, and implements properties that cannot be accessed directly.
Several methods of encapsulation:
(1) Directly privatizing properties, providing getter and setter
Format: self. Property name = property value
If you want to get or set object properties outside the class
Use get, set method.

class user(object):

    def __init__(self, name, age,work):
        self.__name = name
        self.__age = age
        self.__work = work
        
    def get_name(self):       # __Get name method privatization
        return self.__name

    def set_name(self, name):
        self.__name = name   
 #age, work property get, set is the same as name's 

    def __str__(self):
        return self.__name + str(self.__age) + str(self.__work)
      
if __name__ == '__main__':
    u = user("Hee hee",19,"doctor")
    #(1)
    print(u)
    u.name = "xixi"     #It can't be modified or assigned in the outside world, and the operation result still shows the previous one, which is not modified successfully.
    print(u)       #You can still access the privatized properties. You call STR self to access your own properties, because STR self is the method in the class
 

print(u.__name)    #Once privatized, it will be inaccessible and an error will be reported. Using print(u.name) also reports an error


Use the open method:

(1)If you want to modify property values, use theset(Set up) get(Get) method (the public method provided by the class).

     print(u.get_name())
     u.set_name("zhang")
     print(u.get_name())


(2) , directly privatize the property, provide getter and setter, and introduce the global function of property

The property global function, which is used in the defined class, will further operate the encapsulated property to create an illusion to the outside world. It is the same as no privatization when obtaining and modifying.

class user(object):

    def __init__(self, name, age,work):
        self.__name = name
        self.__age = age
        self.__work = work

    def get_name(self):       # __Get name method privatization
        return self.__name

    def set_name(self, name):
        self.__name = name

    def __str__(self):
        return self.__name + str(self.__age) + str(self.__work)

    name = property(get_name, set_name)  # Get u name has no (), which is passed by reference and called.
    #Only the name attribute is written here. Other attributes are the same
     
if __name__ == '__main__':
    u = user("Hee hee",19,"doctor")
  
    print(u.name)
    u.name = "hehe"
    print(u.name)
    print(u)


(3) Using the property decorator to complete the encapsulation

class user(object):

    def __init__(self, name, age,work):
        self.__name = name
        self.__age = age
        self.__work = work

    def get_name(self):       # __Get name method privatization
        return self.__name

    def set_name(self, name):
        self.__name = name
        
	def get_age(self):     
        return self.__age

    def set_age(self, age):
        self.__age = age
        
    def __str__(self):
        return self.__name + str(self.__age) + str(self.__work)


#@The property decorator is used to encapsulate. At this time, the work does not provide the get and set methods
    @property
    def work(self):
        return self.__work
    @work.setter
    def work(self,work):
        self.__work  = work

if __name__ == '__main__':
    u = user("Hee hee",19,"doctor")
    print(u.work)
    u.work = "teacher"
    print(u.work)

Function:

Be careful:

def work(self):
    return self.__work

def work(self,work):
    self.__work  = work

If there are no @ lines, python is an interpreted language (corresponding to a compiled language). There is no method overload. The method name is the same, and the number and type of parameters are different. The first method will be covered by the second same method. The interpreted language will execute line by line. Once there is a line error, the following code will not execute, but the compiled language will execute according to different parameters and types.

Published 12 original articles, won praise 1, visited 268
Private letter follow

Tags: Python Programming Attribute

Posted on Thu, 12 Mar 2020 03:20:01 -0700 by whitchman