Hu Python02

 1. Object-Oriented Advanced

In the previous section we already know object-oriented entry-knowledge, know how to define a class, how to create objects and how to target a message. To be able to make better use of object-oriented programming ideas for program development, we also need the Python object-oriented programming more in-depth understanding.

1. First, let's understand what "private variable", before you add the variable name "-." Private variables can not be inherited, but can no longer invoke external call internally. 

 2. @ property decorator

Value before we discussed the issue of access to properties and methods in Python, although we do not recommend the property to private, but if the property is directly exposed to the outside world is also problematic, such as we have no way to check the validity assigned to the property . We recommend that before the attribute names that begin with a single underscore to imply that property is protected in this way is not recommended for direct access to the outside world, so if you want to access the property through property getter (accessor) and setter (Modifier ) corresponding to the operation method

important point:

     # 1. decorator names, function names needs to be consistent. 

    # 2. property will need to declare, write setter, the order can not be reversed

     # 3. If you want just a little variable is accessed can not be modified, you can use accessor @property

     # 4. If you want to modify the accessor variables can build a modifier, or remove access control

Example: Enter the three numbers (private), find the three numbers and try to change where the two digits

code show as below:

class Number(object):
    def __init__(self,num1,num2,num3):
        self._num1 = num1
        self.__num2 = num2
        self.__num3 = num3
    @property
    def num1(self):
        print(self._num1)
    @num1.setter
    def num1(self.num):
        self._num1 = num
    def SUM(self):
        print(self._num1 + self.__num2 + self.__num3)
        
number = Number(1,2,3)
number.num1 = 1000
number.SUM()

 

example:

Import numpy AS NP
class In Email (Object):
    DEF the __init __ (Self):
        Pass
    DEF zhuce (Self):
        Print ( "Please enter the e-mail address:")
        RES = INPUT ( 'Input')
        Print ( "e-mail address", res, "@ 163.com")
    DEF password (Self):
        Print ( "Please enter your password:")
        mima the iNPUT = ( 'input')
        Print ( "Please enter the password:")
        querenmima the iNPUT = ( 'input')
        IF mima querenmima ==:
            Print ( "password has been set")
        the else:
            Print ( "enter the wrong password, please reset your password")
            
SYX = Email ()
syx.zhuce ()
syx.password ()

 

 
 

In the previous section we already know object-oriented entry-knowledge, know how to define a class, how to create objects and how to target a message. To be able to make better use of object-oriented programming ideas for program development, we also need the Python object-oriented programming more in-depth understanding.

1. First, let's understand what "private variable", before you add the variable name "-." Private variables can not be inherited, but can no longer invoke external call internally. 

 2. @ property decorator

Value before we discussed the issue of access to properties and methods in Python, although we do not recommend the property to private, but if the property is directly exposed to the outside world is also problematic, such as we have no way to check the validity assigned to the property . We recommend that before the attribute names that begin with a single underscore to imply that property is protected in this way is not recommended for direct access to the outside world, so if you want to access the property through property getter (accessor) and setter (Modifier ) corresponding to the operation method

important point:

     # 1. decorator names, function names needs to be consistent. 

    # 2. property will need to declare, write setter, the order can not be reversed

     # 3. If you want just a little variable is accessed can not be modified, you can use accessor @property

     # 4. If you want to modify the accessor variables can build a modifier, or remove access control

Example: Enter the three numbers (private), find the three numbers and try to change where the two digits

code show as below:

class Number(object):
    def __init__(self,num1,num2,num3):
        self._num1 = num1
        self.__num2 = num2
        self.__num3 = num3
    @property
    def num1(self):
        print(self._num1)
    @num1.setter
    def num1(self.num):
        self._num1 = num
    def SUM(self):
        print(self._num1 + self.__num2 + self.__num3)
        
number = Number(1,2,3)
number.num1 = 1000
number.SUM()

 

example:

Import numpy AS NP
class In Email (Object):
    DEF the __init __ (Self):
        Pass
    DEF zhuce (Self):
        Print ( "Please enter the e-mail address:")
        RES = INPUT ( 'Input')
        Print ( "e-mail address", res, "@ 163.com")
    DEF password (Self):
        Print ( "Please enter your password:")
        mima the iNPUT = ( 'input')
        Print ( "Please enter the password:")
        querenmima the iNPUT = ( 'input')
        IF mima querenmima ==:
            Print ( "password has been set")
        the else:
            Print ( "enter the wrong password, please reset your password")
            
SYX = Email ()
syx.zhuce ()
syx.password ()

 

Guess you like

Origin www.cnblogs.com/zhangxiaoqian/p/11317756.html