Learning python package 13 of the type 2

'' '' '' 
'' '
Object-oriented three properties: encapsulation, inheritance, polymorphism
1. Packaging: class or to __ _ attributes are private property, prohibit external calls.
'' '
Class Student (Object):

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

One = Student (' WSX ', 18 is,' M ')

Print (one._Student__name)
Print (one._Student__age)
Print (one._Student__sex)

one._Student__age = 20 is
Print (one._Student__age)

#Print (One .__ name) # error, __ name is privatized, encapsulated, so outside inaccessible, can not find the property
#print (one.name) # error, not the attribute
'' '
above packages, call up more trouble.
Now I will name, age, sex set to private property, but I also want them to access or modify my attributes I specify the interface, how to achieve it?
'' '


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

DEF get_name (Self):
return Self .__ name # this way is inside the class, so we can obtaining the property by way .__ name of

DEF set_name (Self, name):
IF len (name)>. 1:
Self .__ name = name
the else:
Print ( "length of the name must be greater than a length")

DEF get_age (Self) :
return Self .__ Age

DEF set_age (Self, Age):
IF Age> 0 and Age <150:
Self .__ Age = Age
the else:
Print ( "Age input must be greater than 0, less than 150 years")

TWO = student1 ( ' wsx ', 18,' M ')

two.set_name (' A ') # Set through their own interface, it can effectively avoid dirty data
print (two.get_name ()) # get the data through the interface


two.set_age (20) # set through their own interface, you can effectively avoid dirty data
print (two.get_age ()) # get the data through the interface
'' '
so we do from defines the interface of their own attributes, its advantage lies in: the dirty avoid
the problem: using the interface and the point of use is provided a method of data acquisition (one.name = 18 or print (one.name)) compared to setting data,
points more convenient method , is there any way we reach the point of use both methods, while allowing point method calls directly to our interface to the?
'' '
Class STUDENT2 (Object):

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

@Property
DEF name (Self):
return Self .__ name

the @name .setter
DEF name (Self, name):
IF len (name)>. 1:
Self .__ name = name
the else:
print ( "The name must be greater than the length of a length")

@Property
DEF Age (Self):
return Self .__ Age

@ age.setter
DEF Age (Self, Age):
IF Age> 0 and Age <150:
Self .__ Age Age =
the else:
Print ( "Age input must be greater than 0, less than 150 years")


Three STUDENT2 = ( 'WSX', 18 is, 'M')

three.name = 'LY'
Print (three.name)

three.age 170 =
Print (three.age)

'' '
@property: decorator: a responsible way into property to call
@ attribute name .setter: make a way into property, only need to add @property on the get method in this case itself has created another @property decorator, @ attribute name .setter, for the assignment of property values
' ''

Guess you like

Origin www.cnblogs.com/wsxcode/p/12362454.html