Encapsulation of Python Classes
Take the previous section and learn the definition and instantiation of the student class, each of which has its own name and score. Now if you need to print a student's score, you can define the function Print_score ()
The function is a function outside the class, as follows:
1 classStudent:2 def __init__(self, Name, score):3Self.name =name4Self.score =score5 6May = Student (" May", 90)#instantiation of7Peter = Student ("Peter", 85)8 9 defPrint_score (Student):Ten Print("%s ' s score is:%d"%(Student.name,student.score)) One A Print_score (May) -Print_score (Peter)
Since the student instance has this data in its own right, there is no need to access the data from outside functions, and we can define functions that access the data directly within the student class. In this way, the data is "encapsulated" up.
"Encapsulation" is the combination of abstract data and behavior (or function) to form an organic whole (that is, a class); encapsulation is designed to enhance security and simplify programming, and users do not have to understand the specifics of the implementation, but simply use the members of the class through an external interface, a specific access permission.
The functions of these encapsulated data are associated with the student class itself, which we call the method of the class. So how do you define the method of the class?
It is necessary to use the object self itself, referring to the above example, the Print_score () function as a method of Class (Python2.7 version, recommended. Format output):
1 classStudent:2 def __init__(self, Name, score):3Self.name =name4Self.score =score5 6 defPrint_score (self):7 #print ("%s ' s score is:%d"% (Self.name,self.score))8 Print("{self.name} ' s score is: {Self.score}". Format (self=self))#Python 2.7 +. Format Optimization notation9 TenMay = Student (" May", 90) OnePeter = Student ("Peter", 85) A - May.print_score () -Peter.print_score ()
defines a method for a class: except for the first argument is self, the other is the same as the normal function.
instance Call method: only need to call directly on the instance variable, except self does not pass, other parameters normal incoming; Note that if the class method only needs self, no other, when calling the method, only need to instance_name.function _name ()
In this way, we look at the student class from the outside, just need to know that the creation of the instance needs to give name and score, and how to print, is defined within the student class, the data and logic is "encapsulated", the call is easy, but do not know the details of the internal implementation.
Another benefit of encapsulation is the ability to add new methods to the student class; This method can also require a reference, such as the new definition compare function, as follows
1 classStudent:2 def __init__(self, Name, score):3Self.name =name4Self.score =score5 6 defPrint_score (self):7 #print ("%s ' s score is:%d"% (Self.name,self.score))8 Print("{self.name} ' s score is: {Self.score}". Format (self=self))#Python 2.7 +. Format Optimization notation9 Ten defCompare (self,s): One ifSelf.score>S: A Print("Better than%d"%(s)) - elifself.score==S: - Print("Equal%d"%(s)) the Else: - Print("lower than%d"%(s)) - -May = Student (" May", 90) +Peter = Student ("Peter", 85) - + May.print_score () A Peter.print_score () at -May.compare (100) -May.compare (90) -May.compare (89)
Python Learning (vii) Object-oriented-encapsulation