class Student(object):
def __init__(self,name,score):
self.name = name
self.score = score
li = Student('libai','')
print(li.name)
print(li.score)

面向对象编程的一个重要特点就是数据封装。在上面的Student类中,每个实例就拥有各自的namescore这些数据。我们可以通过函数来访问这些数据,比如打印一个学生的成绩:

def print_score(student):
print('{}的成绩是:{}'.format(student.name,student.score))
print_score(li)

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

class Student(object):
def __init__(self,name,score):
self.name = name
self.score = score def print_score(self):
print('{}的成绩是:{}'.format(self.name, self.score))

要定义一个方法,除了第一个参数是self外,其他和普通函数一样。要调用一个方法,只需要在实例变量上直接调用,除了self不用传递,其他参数正常传入:

li.print_score()

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

封装的另一个好处是可以给Student类增加新的方法,比如get_grade

class Student(object):
def __init__(self,name,score):
self.name = name
self.score = score
def print_score(self):
print('{}的成绩是:{}'.format(self.name, self.score)) def get_grade(self):
if self.score >= 90:
return 'A'
elif self.score >=60:
return 'B'
else:
return 'C'

同样的,get_grade方法可以直接在实例变量上调用,不需要知道内部实现细节:

class Student(object):
def __init__(self,name,score):
self.name = name
self.score = score
def print_score(self):
print('{}的成绩是:{}'.format(self.name, self.score)) def get_grade(self):
if self.score >= 90:
return 'A'
elif self.score >= 60:
return 'B'
else:
return 'C'
wang = Student('WangDong',92)
liang = Student('LiangMeng',46)
print(wang.name,wang.get_grade())
print(liang.name,liang.get_grade())

小结:

类是创建实例的模板,而实例则是一个一个具体的对象,各个实例拥有的数据都互相独立,互不影响;

方法就是与实例绑定的函数,和普通函数不同,方法可以直接访问实例的数据;

通过在实例上调用方法,我们就直接操作了对象内部的数据,但无需知道方法内部的实现细节。

和静态语言不同,Python允许对实例变量绑定任何数据,也就是说,对于两个实例变量,虽然它们都是同一个类的不同实例,但拥有的变量名称都可能不同:

>>> bart = Student('Bart Simpson', 59)
>>> lisa = Student('Lisa Simpson', 87)
>>> bart.age = 8
>>> bart.age
8
>>> lisa.age
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Student' object has no attribute 'age'

参考源码

student.py

最新文章

  1. 解决JS中各浏览器Date格式不兼容的问题
  2. Windows Azure HandBook (3) 浅谈Azure安全性
  3. html 通用 遮罩弹出层 弹出后 支持跳转页面
  4. HDU 2209 翻纸牌游戏 状态BFS
  5. Python学习教程(learning Python)--2.3.4Python函数返回值
  6. iOS英文 汉化,如调用相册,相机改“cancel”,“photos”为“取消”,“相机”
  7. QObject的event函数就可以改写对消息的处理
  8. 查看mysql apache php nginx的编译参数
  9. Route@简单应用
  10. MySQL连接方式及大小写问题
  11. Yii2 Restful Api 401
  12. 【Spark篇】---Spark中Shuffle机制,SparkShuffle和SortShuffle
  13. Mybatis的JDBC提交设置/关闭mysql自动提交------关于mysql自动提交引发的惨剧
  14. 【python】——购物车
  15. centos下配置nginx遇到的一些基本的坑
  16. 客户端连接SQL报&quot;Cannot Generate SSPI Context&quot;错误
  17. [javaSE] 网络编程(UDP通信)
  18. CF 1117 E. Decypher the String
  19. Linux命令-查看进程命令:pstree
  20. 【CF375C】Circling Round Treasures

热门文章

  1. 程序猿想聊天 - 創問 4C 團隊教練心得(一)
  2. json格式处理及扩展
  3. 给dao层注入jdbcTemplate时的一个强行bug(jdbcDaoSupport不要随便用!用了要记得!)
  4. MySQL索引初探
  5. 第二章 Linux目录学习
  6. DVWA 黑客攻防实战(十五) 绕过内容安全策略 Content Security Policy (CSP) Bypass
  7. Visual Studio插件开发基础
  8. .net解析csv(C#导表工具)
  9. SSH服务与tcp wrappers实验
  10. 【spring源码分析】IOC容器初始化(三)