要求:
学生类的创建:
class Student:
count = 0
def __init__(self,name,age):
self.name = name
self.__age = age
self.__score1 = {}
def base(self):
print("当前学生数量为%d\n"%self.count)
print("姓名:%s"%self.name)
print("年龄:%s"%self.__age)
def __getitem__(self, item):
return self.__score1.get(item)
def __setitem__(self, key, value):
self.__score1[key] = value
def score2(self):
for kv in self.__score1.items():
print(kv)
max_pr = max(self.__score1,key = self.__score1.get)
print("分数最好的科目为:")
print(max_pr)
班长类:
由于班长也属于学生,具有学生类的功能,只需继承学生类,创建班长独有的方法
class Moin(Student):
def spu(self):
time = input()
print("今天将在%a召开班级会议,请大家按时参加班级会议"%time)
完整代码及示例:
class Student:
count = 0
def __init__(self,name,age):
self.name = name
self.__age = age
self.__score1 = {}
def base(self):
print("当前学生数量为%d\n"%self.count)
print("姓名:%s"%self.name)
print("年龄:%s"%self.__age)
def __getitem__(self, item):
return self.__score1.get(item)
def __setitem__(self, key, value):
self.__score1[key] = value
def score2(self):
for kv in self.__score1.items():
print(kv)
max_pr = max(self.__score1,key = self.__score1.get)
print("分数最好的科目为:")
print(max_pr)
class Moin(Student):
def spu(self):
time = input()
print("今天将在%a召开班级会议,请大家按时参加班级会议"%time)
bigboss = Moin("班长","20")
Student.count += 1
bigboss.base()
bigboss["语文"] = 120
bigboss["数学"] = 130
bigboss["英语"] = 140
bigboss.score2()
bigboss.spu()
jack = Student("学生1","20")
Student.count += 1
jack.base()
jack["语文"] = 110
jack["数学"] = 140
jack["英语"] = 130
jack.score2()
运行结果:
C:\Users\86178\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/86178/PycharmProjects/pythonProject/实验3.py
当前学生数量为1
姓名:班长
年龄:20
('语文', 120)
('数学', 130)
('英语', 140)
分数最好的科目为:
英语
5:00
今天将在5:00召开班级会议,请大家按时参加班级会议
当前学生数量为2
姓名:学生1
年龄:20
('语文', 110)
('数学', 140)
('英语', 130)
分数最好的科目为:
数学
Process finished with exit code 0