File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+ # -*- coding: utf-8 -*-
3+
4+ ' 练习面向对象的类和实例 '
5+
6+ __author__ = 'sergiojune'
7+
8+ # 定义学生实例
9+
10+
11+ class Student (object ):
12+ def __init__ (self , name , score ):
13+ ' 初始化实例,当创建实例时被调用这个函数,所以在实例化时需要传入这个函数的参数 '
14+ self .name = name
15+ self .score = score
16+
17+ def get_grade (self ):
18+ '对数据进行封装'
19+ print ('my name is %s' % self .name )
20+ if self .score > 90 :
21+ print ('your grade is A' )
22+ elif self .score > 75 :
23+ print ('your grade is B' )
24+ else :
25+ print ('your grade is C' )
26+
27+
28+ # bart = Student()
29+ # print(bart)
30+ # # 给实例初始化名字属性和成绩
31+ # bart.name = 'bart june'
32+ # bart.score = 98
33+ # print(Student)
34+ # print(bart.name)
35+ # print(bart.score)
36+ # 实例化
37+ tom = Student ('Tom' , 87 )
38+ print (tom .name )
39+ print (tom .score )
40+ bob = Student ('Bob' , 65 )
41+ # 调用实例方法
42+ bob .get_grade ()
You can’t perform that action at this time.
0 commit comments