Wrappers and built-in functions

Packaging benefits:

【Package】

         Hide the properties and implementation details of the object, and only provide public access to the outside world.

【benefit】 

1. Isolate changes; 

2. Easy to use;

3. Improve reusability; 

4. Improve security;

【Encapsulation principle】

      1. Hide the content that does not need to be provided externally;

      2. Hide the properties and provide public methods to access them.

definition:

package
# Encapsulation in a broad sense: static and dynamic properties belonging to a class always appear in a class
                # The usage used is always called with the class name or object name
# Encapsulation in a narrow sense: it is to privatize variables/methods, which cannot be used directly outside the class and in subclasses

Private variable reference:
# class A:
# STATIC = 'aaa' # static variable
# __S = 'bbb' # private static variable
#     def wahaha(self):
#         print(A.__S) # _A__S
# print(A.STATIC)
# print(A.__dict__)
# print(A._A__S) # call private variables outside the class
# a = A()
# a.wahaha()
# A.__B = 'ccc' #Added a static variable outside the class
# print(A.__dict__) #We cannot create a "private" variable outside a class
# print(A.__B)

 

One: When calling it, you can define it as a method, and then print the private variable

Call private properties inside, write a function
# class B:
#     def __init__(self,name,pwd):
#         self.name = name
# self.__pwd = pwd # You can also create private properties of objects
#
#     def get_pwd(self):
#         print(self.__pwd)
# b = B('alex','alex3714')
# b.qqxing()
# print(b.name)
# print(b._B__pwd) # When outside the class, we can't directly use the private properties of the object
# b.get_pwd()

Two: Summary of private variables:

private de static variable object property method
    #Private can only be defined and used inside the class
    # __name
    # use private static variable_class_name__private_name outside the class
    # Private names cannot be inherited by subclasses

Correspondence between Java and Python

java
    # private private - __variable name
    # protect protected - N/A
    # public public - normal variables

Example 1:

Room type: All price area
# class Room:
#     def __init__(self,owner,price,length,width,height):
#         self.owner = owner
# self.__price_single = price #unit price
#         self.__length = length
#         self.__width = width
#         self.height = height
#
#     def get_area(self):
#         return self.__length * self.__width
#
#     def get_price(self):
#         return self.__price_single * self.get_area()
#
# alex = Room('alex',1000000,2,1,0.8)
# print(alex.get_area())
# print(alex.get_price())

Note: Defining method call private variables

Disguise methods as properties

instance class when instantiated

But when calling, use the method # print(wang.bmi)

Class able to calculate the BMI index of the human body
# class Person:
#     def __init__(self,name,height,weight):
#         self.name = name
#         self.__height = height
#         self.__weight = weight
#
# @property # Disguise a method as a property
#     def bmi(self):
#         return self.__weight / (self.__height**2)
#
# wang = Person('Prince',1.77,69)
# print (wang.bmi)

To put it bluntly, it is to disguise the attributes that are not to be shown to the user, and then call the method

 

Small application: define a few method calls, call a few plus a few @property

# Circle class: r area area perimeter perimeter
# from math import pi
# class Circle:
#     def __init__(self,r):
#         self.r = r
#     @property
#     def area(self):
#         return self.r*self.r*pi
#     @property
#     def perimeter(self):
#         return 2*pi*self.r
#
# c = Circle(10)
# print(c.area)
# print(c.perimeter)

 

@classmethod installs ordinary methods as class methods, which is actually self. It is still called by class methods. To put it bluntly, it is regarded as a parameter.

lass Goods:
    __discount = 0.8
    def  __init__(self,name,price):
        self.name = name
        self.__price = price

    @property
    def price(self):
        return self.__price*Goods.__discount

    @classmethod #Talk about a common method decorated as a class method
    def change_discount(cls, new_dis ): # class method
        cls.__discount = new_dis call class method = later parameter
 Goods.change_discount(1)
cig = Goods('cigrette',20)
print(cig.price)
cig.change_discount(0.2)
print(cig.price)
# cig.change_discount(1)
# print(cig.price)

annotation:

#Class methods are special methods decorated by @classmethod
    #After being decorated, the method receives a class as a parameter by default
    # All operations after that can only be related to static variables in the class and should not be related to objects
# Both the class name and the object name can directly call the class method

 

class Student:
    def __init__(self,name):
        self.name = name

     @staticmethod #Decorate a function that does not require either the self parameter nor the cls parameter
     def login(a, b , c ): # Ordinary function
        usr = input('username>>>')
        pwd = input('password>>>')
        if usr == 'alex' and pwd == '123':
            obj = Student(usr)
            return obj
# student login
# username and password properties
ret = Student.login (1,2,3)
print (ret)

Use this function to disguise it as a function

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325863424&siteId=291194637