CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day08/student.py
Views: 729
1
"""
2
定义和使用学生类
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-08
7
"""
8
9
10
def _foo():
11
print('test')
12
13
14
class Student(object):
15
16
# __init__是一个特殊方法用于在创建对象时进行初始化操作
17
# 通过这个方法我们可以为学生对象绑定name和age两个属性
18
def __init__(self, name, age):
19
self.name = name
20
self.age = age
21
22
def study(self, course_name):
23
print('%s正在学习%s.' % (self.name, course_name))
24
25
# PEP 8要求标识符的名字用全小写多个单词用下划线连接
26
# 但是很多程序员和公司更倾向于使用驼峰命名法(驼峰标识)
27
def watch_av(self):
28
if self.age < 18:
29
print('%s只能观看《熊出没》.' % self.name)
30
else:
31
print('%s正在观看岛国大电影.' % self.name)
32
33
34
def main():
35
stu1 = Student('骆昊', 38)
36
stu1.study('Python程序设计')
37
stu1.watch_av()
38
stu2 = Student('王大锤', 15)
39
stu2.study('思想品德')
40
stu2.watch_av()
41
42
43
if __name__ == '__main__':
44
main()
45
46