码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习(七)面向对象 ——封装

时间:2015-04-10 21:45:25      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

Python 类的封装

 

  承接上一节,学了Student类的定义及实例化,每个实例都拥有各自的name和score。现在若需要打印一个学生的成绩,就可定义函数 print_score()

  该函数为类外的函数,如下:

 1 class Student:
 2     def __init__(self, name, score):
 3         self.name = name
 4         self.score = score
 5 
 6 May = Student("May",90)        # 实例化
 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)

  既然Student实例本身就拥有这些数据,要访问这些数据,就没有必要从外面的函数去访问,我们可以直接在Student类的内部定义访问数据的函数。这样,就把数据给“封装”起来了。

  “封装”就是将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体(即类);封装的目的是增强安全性和简化编程,使用者不必了解具体的实现细节,而只是要通过外部接口,一特定的访问权限来使用类的成员。

  而这些封装数据的函数是和Student类本身是关联起来的,我们称之为类的方法。那如何定义类的方法呢?

  就要用到对象 self 本身,参考上例,把 print_score() 函数写为类的方法(Python2.7之后的版本,推荐.format 输出写法):

 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优化写法
 9 
10 May = Student("May",90)        
11 Peter = Student("Peter",85)        
12 
13 May.print_score()
14 Peter.print_score()

  定义类的方法:除了第一个参数是self外,其他和普通函数一样。

  实例调用方法:只需要在实例变量上直接调用,除了self不用传递,其他参数正常传入;注意,若类的方法仅需要self,不需要其他,调用该方法时,仅需 instance_name.function_name()

  这样一来,我们从外部看Student类,就只需要知道,创建实例需要给出name和score,而如何打印,都是在Student类的内部定义的,这些数据和逻辑被“封装”起来了,调用很容易,但却不用知道内部实现的细节。

  封装的另一个好处是可以给Student类增加新的方法;这边的方法也可以要求传参,如新增定义compare 函数,如下

 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优化写法
 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)

 

Python学习(七)面向对象 ——封装

标签:

原文地址:http://www.cnblogs.com/feeland/p/4415645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!