Object-oriented programming - encapsulation, decorator (property,classmethod,staticmethod)

1. Packaging

1. Encapsulation refers to the integration of data and functions

2. On the basis of encapsulation, I can hide the attributes installed in the object or class
    Notice:
(1) When defining a class or initializing an object, adding __ before an attribute will hide the attribute
But the hidden start is just a kind of deformation_class name__ attribute name, and does not really hide
(2) The deformation operation is the deformation that occurs when the grammar is scanned in the class definition stage, and the attributes starting with __ added after the class definition will not be deformed
(3) The concealment is external but not internal
(4) In inheritance, if the parent class does not want the subclass to override its own method, can the method be defined as private? ? ?
# example 1
# class Student:
#     __school = "oldboy"  # _Student__school = "oldboy"
#
#     def __init__(obj, x, y, z):
#         obj.__name = x  # obj._Student__name = x
#         obj.age = y
#         obj.gender = z
#
#     def __choose(self):  # obj._Student__choose
#         print("%s course selection" %self.name)
#
#
# stu_obj1 = Student("Feng Lunzi", 18, "female")
# stu_obj1.__x=111
# print(stu_obj1.__dict__)
# print(stu_obj1.__x)

# print(stu_obj1.__dict__)
# print(stu_obj1._Student__name)

# print(Student.__dict__)
# print(Student._Student__school)
# print(stu_obj1._Student__school)
The meaning of hidden attributes
1. The meaning of hiding data attributes is: open the interface in the class, let outside users operate the attribute value through the interface, we can attach any logic to the interface
To strictly control the operation of attributes by external users
# class Student:
#     __school = "oldboy"  # _Student__school = "oldboy"
#
#     def __init__(obj, x, y, z):
#         obj.__name = x  # obj._Student__name = x
#         obj.__age = y
#         obj.gender = z
#
#     def __choose(self):  # obj._Student__choose
#         print("%s course selection" % self.name)
#
#     def get_name(self):
#         print(self.__name)  # print(self._Student__name)
#
#     def set_age(self,x):
#         if type(x) is not int:
#             print("Age must be an integer, stupid")
#             return
#         self.__age = x
#
#     def get_age(self):
#         print(self.__age)
#
#     def del_age(self):
#         del self.__age
#
# stu_obj1 = Student("Feng Lunzi", 18, "female")
# # stu_obj1.get_name()
#
#
# # stu_obj1.set_age("asfdasfdasfafd")
# stu_obj1.set_age(19)
# stu_obj1.get_age()
# # print(stu_obj1.__dict__)
2. Hide functional properties: isolate complexity
# class ATM:
#     def __card(self):
#         print('card')
#     def __auth(self):
#         print('User Authentication')
#     def __input(self):
#         print('Enter withdrawal amount')
#     def __print_bill(self):
#         print('print bill')
#     def __take_money(self):
#         print('withdraw money')
#
#     def withdraw(self):
#         self.__card()
#         self.__auth()
#         self.__input()
#         self.__print_bill()
#         self.__take_money()
#
# a=ATM()
# a.withdraw()

2. Decorator

  1, property

# example 1
# class People:
#     def __init__(self, name, height, weight):
#         self.name = name
#         self.height = height
#         self.weight = weight
#
#     @property
#     def bmi(self):
#         return self.weight / (self.height ** 2)
#
#
# p = People('egon', 1.81, 70)
# p.height = 1.84
# print(p.bmi())

 

# Example 2
# class Student:
#     __school = "oldboy"  # _Student__school = "oldboy"
#
#     def __init__(obj, x, y, z):
#         obj.__name = x
#         obj.__age = y
#         obj.gender = z
#
#     def get_name(self):
#         print("Access control")
#         return self.__name
#
#     def set_name(self,x):
#         print("assignment control")
#         self.__name = x
#
#     def del_name(self):
#         print("delete control")
#         del self.__name
#
#     def get_age(self):
#         return self.__age
#
#     def set_age(self, x):
#         if type(x) is not int:
#             print("Age must be an integer, stupid")
#             return
#         self.__age = x
#
#     def del_age(self):
#         print("do not delete")
#
#
#     age = property(get_age, set_age, del_age)
#     name = property(get_name, set_name, del_name)
#
#
# stu_obj1 = Student("Feng Lunzi", 18, "female")
#
# # print(stu_obj1.age)
# # stu_obj1.age = "19"
# # del stu_obj1.age
# # print(stu_obj1.age)
#
#
# print(stu_obj1.name)
# # stu_obj1.name="EGON"
# # del stu_obj1.name

 

# Example 3:
class Student:
    __school = "oldboy"  # _Student__school = "oldboy"

    def __init__(obj, x, y, z):
        obj.__name = x
        obj.__age = y
        obj.gender = z

    @property
    def name(self):
        print("Access control")
        return self.__name

    @name.setter
    def name(self, x):
        print("assignment control")
        self.__name = x

    @name.deleter
    def name(self):
        print("delete control")
        del self.__name


stu_obj1 = Student("Feng Lunzi", 18, "female")

stu_obj1.name

 

2. Binding and non-binding

Defined functions in a class
--- Binding method: whoever calls it will be passed in as the first parameter
(1) The method bound to the object: The function defined in the class is the method bound to the object by default, it should be called by the object, and the object will be passed in as the first parameter
(2) The method bound to the class: add a decorator @classmethod to the function in the class, the function is bound to the class, it should be called by the class, and the class will be passed in as the first parameter

---Non-bound method: It is neither bound to the class nor to the object, it is an ordinary function, which can be called by anyone, without the effect of automatic parameter transfer,
Add the decorator @staticmethod on the function
# class People:
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#
#     def tell_info(self):
#         print("<%s:%s>" % (self.name, self.age))
#
#     @classmethod
#     def f1(cls):
#         print(cls)
#
#     @staticmethod
#     def f2(x,y,z):
#         print(x,y,z)
#
#
# p1=People('egon',18)
# # p1.tell_info()
#
# # print(p1.tell_info)
# # print(People.f1)
#
# # People.f1()
#
# # print(p1.f2)
# # print(People.f2)
# p1.f2(1,2,3)
# People.f2(1,2,3)

 

# example 1
import uuid
import settings
##The content in the settings: IP='106.78.13' PORT=3306
class MySQL: def __init__(self,ip,port): self.mid = self.__create_id() self.ip = ip self.port = port def tell_info(self): print("%s:<%s:%s>" %(self.mid,self.ip,self.port)) @staticmethod def __create_id(): return uuid.uuid4() @classmethod def from_conf(cls): return cls(settings.IP, settings.PORT) # obj = MySQL("10.10.11.11",3306) # obj.tell_info() obj1=MySQL.from_conf() obj1.tell_info()

 

Posted by lightkeepr on Tue, 24 May 2022 13:45:36 +0300