python base - of an encapsulated object-oriented

# In python with double underlined, the beginning of the way hidden attribute (arranged private) 
# but in fact this is just a deforming operation, and only in the class definition phase may be deformed 
# class as the beginning of all double underline _ _x automatically formed when the class definition: _ __x the class name. 
class A:
     __N = 0
     # attribute class should be shared, but the grammar data is provided to the private attribute class as __N becomes _A__N 
    DEF  the __init__ (Self):
        self.__X = 10# self._A__X
    def __foo(self): # _A__foo
        print('from A')
    def bar(self):
        Self. __foo () # only be accessed by the form of the inner class __foo 
        Print (Self. __N )
 # A._A__ is accessible to 
# this, the outside is not accessible by the name __x to 
a = a ()
a.bar()
# print(a.__N)
print(a._A__N)
print(a.__dict__)

a.__Y = 1
print(a.__dict__)

# Inheriting the parent class if you do not want subclasses to cover their own methods, you can define the method 
# as private 
class A:
     DEF FA (Self):
         Print ( ' from A ' )
     DEF the Test (Self):
        self.fa ()
class B(A):
    def fa(self):
        print('from B')
b = B()
b.test()

# The fa defined private i.e. __fa 
class A:
     DEF  __fa (Self):
         Print ( ' from A ' )
     DEF Test (Self):
        self.__fa()
        print(self.__fa)
class B(A):
    def __fa(self):
        print('from B')
b = B()
b.test()
# We can look at the implementation process, the implementation of test- this time self is the object b - looking __fa method (as we said, __ fa .__ FA will become _A) - 
# First, a quick look in the class B, is not found, - then look in the parent class A, - execution

# True meaning of the package is that a clear distinction between inside and outside, the package can be used directly within the property, and can not be used directly on the outside, but the definition of 
# purpose is to use the property after all, want to use an external class hidden attributes, we need their open interfaces, 
# 1, data encapsulation, data hiding is not an end, hide and provides an interface to operate the data, then we can at 
#    additional restrictions on the return data manipulation interface, in order to complete a strict control over data attributes 
#        analogous to various accessories on the computer, the data is encapsulated, we can change the configuration through the operation of the computer hardware, 
#        example we want to add a memory, only you need to know the interface, frequency and other parameters, without knowing the memory What is material, 
#        do not know the internal memory particles constitute the 
class Teacher:
     DEF  __init__ (Self, name, Age):
        self.__name = name
        self.__age = age
        self.set_info(name,age)
    DEF tell_info (Self):
         Print ( ' name:% S, Age:% S ' .% (Self __name__ ., Self __age ))
     DEF set_info (Self, name, Age):
         IF  Not the isinstance (name, STR):
             The raise TypeError ( " name must be a string type " )
         IF  not isinstance (Age, int):
             The raise TypeError ( " Age must be an integer type " )
        self.__name = name
        self.__age = age
t = Teacher('wang',21)
t.tell_info()
t.set_info('cong',21)
t.tell_info()
# 2, packaging method: The purpose is to isolate the complexity of the 
#    as we play on the computer, we do not need to know the working principle of the computer, just you need to know how he used it

 

Guess you like

Origin www.cnblogs.com/cong12586/p/11366512.html