Private properties and methods

# Private property: Two underline attribute name in front, then this property is called private property 
# private methods: in front of the method name has two underscore, then this method is called private method

Features # private property and private methods:
# private attributes and private methods can only be used in the current class, not the outer, or other classes classes (subclass) used inside the


class the Person (Object):
DEF the __init __ (Self, name, Age):
the self.name name =
Self .__ = Age Age

DEF Show (Self):
Print (the self.name, Self .__ Age)

DEF __myage (Self):
Print (Self .__ Age)

S1 = the Person ( 'WJ', 24)
Print (s1.name)
# Print (S1. __age)
s1.show ()
# s1 .__ myAge ()

after the name of private property wrapper # view objects, view object properties
res = s1 .__ dict__ # dict__ object name .__
Print (RES)
# View class methods and properties, you can know the private methods and private property package name
res = Person .__ dict__ # dict_ class name .__
Print (RES)

Know after you can directly point to access ()
s1._Person__myage ()
Print (s1._Person__age)

Guess you like

Origin www.cnblogs.com/wjun0/p/11515398.html