2018-04-27 00:00:22 +08:00
|
|
|
"""
|
|
|
|
定义和使用学生类
|
|
|
|
|
|
|
|
Version: 0.1
|
|
|
|
Author: 骆昊
|
|
|
|
Date: 2018-03-08
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def _foo():
|
2019-05-03 21:17:36 +08:00
|
|
|
print('test')
|
2018-04-27 00:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Student(object):
|
|
|
|
|
2019-05-03 21:17:36 +08:00
|
|
|
# __init__是一个特殊方法用于在创建对象时进行初始化操作
|
|
|
|
# 通过这个方法我们可以为学生对象绑定name和age两个属性
|
|
|
|
def __init__(self, name, age):
|
|
|
|
self.name = name
|
|
|
|
self.age = age
|
2018-04-27 00:00:22 +08:00
|
|
|
|
2019-05-03 21:17:36 +08:00
|
|
|
def study(self, course_name):
|
|
|
|
print('%s正在学习%s.' % (self.name, course_name))
|
2018-04-27 00:00:22 +08:00
|
|
|
|
2019-05-03 21:17:36 +08:00
|
|
|
# PEP 8要求标识符的名字用全小写多个单词用下划线连接
|
|
|
|
# 但是很多程序员和公司更倾向于使用驼峰命名法(驼峰标识)
|
|
|
|
def watch_av(self):
|
|
|
|
if self.age < 18:
|
|
|
|
print('%s只能观看《熊出没》.' % self.name)
|
|
|
|
else:
|
2019-06-13 23:45:07 +08:00
|
|
|
print('%s正在观看岛国大电影.' % self.name)
|
2018-04-27 00:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-05-03 21:17:36 +08:00
|
|
|
stu1 = Student('骆昊', 38)
|
|
|
|
stu1.study('Python程序设计')
|
|
|
|
stu1.watch_av()
|
|
|
|
stu2 = Student('王大锤', 15)
|
|
|
|
stu2.study('思想品德')
|
|
|
|
stu2.watch_av()
|
2018-04-27 00:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-05-03 21:17:36 +08:00
|
|
|
main()
|