The face of the object. Package

Defined package

Encapsulation is a characteristic of the object-oriented characteristics of the three core

The piece of data is encapsulated with a sealing function and packaged, the package can be controlled by the interface

Package <========> Integration

 

Interface hidden method

Before the property name in the class plus __ prefix will implement a hidden attribute the effect of foreign

 

 Note: The hidden nature of the deformation

After adding the prefix attribute __ hidden outside, but in fact essentially

 

Foo class: 
    __X. 1 = # attributes _Foo__x detecting the beginning of the syntax converted into double underlined __X _Foo__x form (__ _ class name attribute name) before the class definition phase 

    DEF __f1 (Self): # _Foo__f1    
        Print ( ' the Test from ') 


Print (Foo .__ dict__) 
Print (Foo._Foo__x) 
Print (Foo._Foo__f1) 


preventing access to the beginning of the two-decline line is directly outside the class attribute __x, but knowing the class name and property name can spell the name : _ __ attribute class name, then you can visit, such as Foo._A__N, 
so that this operation does not restrict external access on the ground in the strict sense, just a deformation of the grammatical meaning.

  

Hidden just outside

Foo class: 
    __X = 1 = 1 # _Foo__x outside this hidden within wrong, because properties will be unified at the beginning of __ deformation occurs when the body type checking code syntax, 

    DEF __f1 (Self): # _Foo__f1 each place are transformed into a form 
        Print ( 'from Test') 

    DEF F2 (Self): 
        Print (Self .__ X) Print # (self._Foo__x) 
        Print (Self .__ F1) Print # (self._Foo__f1) 

Print (Foo .__ X) 
Print (F1 Foo .__ ) 
obj = Foo () 
obj.f2 ()

  

Foo class: 
    __X _Foo__x = = #. 1. 1 

    DEF __f1 (Self): # _Foo__f1 
        Print ( 'from Test') 

    DEF F2 (Self): 
        Print (Self .__ X) Print # (self._Foo__x) 
        Print (Self .__ F1) Print # (self._Foo__f1) 

Foo Y = .__. 3 
Print (Foo .__ dict__ magic) 
Print (Foo .__ Y) 

class Foo: 
    __X _Foo__x = = #. 1. 1 

    DEF the __init __ (Self, name, Age): 
        Self .__ name = name 
        Self Age Age = .__ 

obj = Foo ( 'Egon', 18 is) 
Print (.__ dict__ magic obj) 
Print (obj.name, obj.age) 


warping operation will only occur once (defined above) to check the grammar class body when in this __ after the beginning of the attribute definitions are not deformed

  

Hidden Role

Not to define it also hides not defined, finally need of.

  • Control of the input and output interface

# Designer: 
class People: 
    DEF __init __ (Self, name): 
        Self .__ name = name 

    DEF get_name (Self): 
        # can be accessed indirectly through the interface to the name of the property 
        Print (Self .__ name) 

    DEF set_name (Self, Val ): 
        IF of the type (Val) IS not str: 
            Print ( 'must pass the string type') 
            return 
        Self .__ name = Val 

# users: 
obj = People ( 'Egon') 
# Print (obj.name) can not be directly used # name attribute 
# obj.name = 'EGON' # can not directly change the hidden attribute 
obj.set_name ( "ha") 
obj.get_name () 

hidden data attribute (data hiding) limits the class of external direct operations on the data, then the class should provide an external interface to allow indirect operation data classes, interfaces above may additionally additional logic to strictly control the operation of the data

  

Isolation Complexity:

  • Hidden attribute data (hidden data) can be clearly distinguished explicit attribute implicit attribute
  • In order to distinguish the properties available to a user with a user attribute unavailable
  • Only available attributes displayed for the user, reduce the complexity of the user interface

Guess you like

Origin www.cnblogs.com/Tornadoes-Destroy-Parking-Lots/p/12662632.html