Python Learning (7) Object-oriented-encapsulation, python object-oriented

Source: Internet
Author: User

Python Learning (7) Object-oriented-encapsulation, python object-oriented
Python class Encapsulation

 

Taking on the definition and instantiation of the Student Class in the previous section, each instance has its own name and score. Now if you need to print a student's score, you can define the function print_score ()

This function is an out-of-class function, as follows:

1 class Student: 2 def _ init _ (self, name, score): 3 self. name = name 4 self. score = score 5 6 May = Student ("May", 90) # instantiate 7 Peter = Student ("Peter", 85) 8 9 def print_score (Student ): 10 print ("% s's score is: % d" % (Student. name, Student. (score) 11 12 print_score (May) 13 print_score (Peter)

Since the Student instance itself has the data, to access the data, there is no need to access the data from external functions. We can directly define the function to access the data within the Student class. In this way, the data is encapsulated.

"Encapsulation" is to combine abstract data with behaviors (or functions) to form an organic whole (class). The purpose of encapsulation is to enhance security and simplify programming, the user does not need to know the specific implementation details, but only needs to use the class members through the external interface and a specific access permission.

These data encapsulation functions are associated with the Student class itself, which is called the class method. So how to define the class method?

We need to use the object self itself. refer to the previous example to write the print_score () function as a class method (for Versions later than Python2.7, we recommend that you write the. format output method ):

1 class Student: 2 def _ init _ (self, name, score): 3 self. name = name 4 self. score = score 5 6 def print_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 9 10 May = Student ("May", 90) 11 Peter = Student ("Peter", 85) 12 13 May. print_score () 14 Peter. print_score ()

  Methods for defining classes:Except that the first parameter is self, other functions are the same.

  Instance call method:You only need to call the instance variable directly, except self, and other parameters are passed in normally. Note that if the class method only needs self and no other parameters, when you call this method, only instance_name.function_name () is required ()

In this way, we can look at the Student class from the external, we only need to know that the name and score need to be given to create an instance, and how to print them is defined internally in the Student class, the data and logic are "encapsulated", so it is easy to call, but you do not need to know the details of the internal implementation.

Another advantage of encapsulation is that a new method can be added to the Student class. The method here can also require passing parameters, such as the newly added compare function, as shown below:

1 class Student: 2 def _ init _ (self, name, score): 3 self. name = name 4 self. score = score 5 6 def print_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 9 10 def compare (self, s): 11 if self. score> s: 12 print ("better than % d" % (s) 13 elif self. score = s: 14 print ("equal % d" % (s) 15 else: 16 print ("lower than % d" % (s )) 17 18 May = Student ("May", 90) 19 Peter = Student ("Peter", 85) 20 21 May. print_score () 22 Peter. print_score () 23 24 May. compare (100) 25 May. compare (90) 26 May. compare (89)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.