Skip to content

Commit 4d240ee

Browse files
authored
练习面向对象的类和实例
1 parent bf6dc15 commit 4d240ee

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test16.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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()

0 commit comments

Comments
 (0)