0.定义⼀个学⽣类。有属性:姓名、年龄、成绩(语⽂,数学,英语)[每课成绩的类型为整数]
⽅法: a. 获取学⽣的姓名:getname() b. 获取学⽣的年龄:getage()
c. 返回3⻔科⽬中最⾼的分数。get_course()
class Student:
"""学生类"""
def __init__(self, name, age):
self.name = name
self.age = age
self.scores = {'chinese': 80, 'math': 85, 'english': 75}
def get_name(self):
return self.name
def get_age(self):
return self.age
def get_course(self):
max_score = self.scores['chinese']
for score in self.scores.values():
if max_score < score:
max_score = score
return max_score
if __name__ == '__main__':
joe = Student('jerry', 18)
print(joe.get_name())
print(joe.get_age())
print(joe.get_course())
结果:
jerry
18
85
1.建⽴⼀个汽⻋类Auto,包括轮胎个数,汽⻋颜⾊,⻋身重量,速度等成员变量,并通过不同的构造⽅法创
建实例。⾄少要求 汽⻋能够加速 减速 停⻋。 再定义⼀个⼩汽⻋类CarAuto 继承Auto 并添加空调、CD等成
员变量 覆盖加速 减速的⽅法
class Auto:
"""汽车类"""
def __init__(self, wheels=4, color='', weight=100, speed=0):
self.wheels = wheels
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self, speed):
self.speed += speed
def slow_down(self, speed):
if self.speed > speed:
self.speed -= speed
else:
self.speed = 0
print('减不下去了')
def stop(self):
self.speed = 0
class CarAuto(Auto):
"""小汽车类"""
def __init__(self, wheels=4, color='', weight=100, speed=0, ac='', cd=''):
super().__init__(wheels, color, weight, speed)
self.ac = ac
self.cd = cd
def speed_up(self, speed):
self.speed += speed
def slow_down(self, speed):
if self.speed > speed:
self.speed -= speed
else:
self.speed = 0
print('减不下去了')
def stop(self):
self.speed = 0
if __name__ == '__main__':
pass
2.创建⼀个名为User 的类,其中包含属性firstname 和lastname ,还有⽤户简介通常会存储的其他⼏个属
性。在类User 中定义⼀个名 为describeuser() 的⽅法,它打印⽤户信息摘要;再定义⼀个名为greetuser() 的
⽅法,它向⽤户发出个性化的问候。
管理员是⼀种特殊的⽤户。编写⼀个名为Admin 的类,让它继承User类。添加⼀个名为privileges 的属性,
⽤于存储⼀个由字符串(如"can add post"、"can delete post"、"can ban user"等)组成的列表。编写⼀个名为
show_privileges()的⽅法,它显示管理员的权限。创建⼀个Admin 实例,并调⽤这个⽅法。
class User:
"""用户类"""
def __init__(self, first_name, last_name, description, hometown):
self.first_name = first_name
self.last_name = last_name
self.description = description
self.hometown = hometown
def describe_user(self):
return '我是%s%s,来自%s,%s' % (self.last_name, self.first_name, self.hometown, self.description)
class Admin(User):
"""管理员类"""
def __init__(self, first_name, last_name, description, hometown, privilege=''):
super().__init__(first_name, last_name, description, hometown)
self.privilege = privilege
def show_privileges(self):
return '%s' % self.privilege
if __name__ == '__main__':
pass
3.创建⼀个Person类,添加⼀个类字段⽤来统计Perosn类的对象的个数
class Person:
"""人类"""
all_person = []
def __init__(self, name, age, sex):
self.name = ''
self.age = 0
self.sex = ''
@classmethod
def add_person(cls):
name = input('name:')
age = input('age:')
sex = input('sex:')
person = Person(name, age, sex)
cls.all_person.append(person)
if __name__ == '__main__':
pass
(尝试)5.写⼀个类,其功能是:1.解析指定的歌词⽂件的内容 2.按时间显示歌词 提示:歌词⽂件的内容⼀般
是按下⾯的格式进⾏存储的。歌词前⾯对应的是时间,在对应的时间点可以显示对应的歌词
[00:00.20]蓝莲花
[00:00.80]没有什么能够阻挡
[00:06.53]你对⾃由地向往
[00:11.59]天⻢⾏空的⽣涯
[00:16.53]你的⼼了⽆牵挂
[02:11.27][01:50.22][00:21.95]穿过幽暗地岁⽉
[02:16.51][01:55.46][00:26.83]也曾感到彷徨
[02:21.81][02:00.60][00:32.30]当你低头地瞬间
[02:26.79][02:05.72][00:37.16]才发觉脚下的路
[02:32.17][00:42.69]⼼中那⾃由地世界
[02:37.20][00:47.58]如此的清澈⾼远
[02:42.32][00:52.72]盛开着永不凋零
[02:47.83][00:57.47]蓝莲花
import pygame
lyrics = {'蓝莲花': [1], '没有什么能够阻挡': [3], '你对⾃由地向往': [5, 9], '天⻢⾏空的⽣涯': [6, 10]}
class Lyric:
"""歌词类"""
song = []
def __init__(self, lyric, time):
self.lyric = lyric
self.time = time
@classmethod
def add_lyric(cls, lyrics):
for lyric,time in lyrics.items():
instance = Lyric(lyric, time)
cls.song.append(instance)
@classmethod
def play_lyric(cls):
timer = 0
is_play = cls.song[0].lyric
while True:
is_print = False
for lyric in cls.song:
if timer in lyric.time:
is_play = lyric.lyric
is_print = True
if is_print:
print(is_play)
pygame.time.delay(1000)
timer += 1
if timer == 10:
break
@classmethod
def find_lyric(cls, time):
timer = time
is_play = ''
is_print = False
flag = False
for _ in range(time, 0, -1):
for lyric in cls.song:
if timer in lyric.time:
is_play = lyric.lyric
is_print = True
flag = True
if flag:
break
timer -= 1
if is_print:
print(is_play)
if __name__ == '__main__':
Lyric.add_lyric(lyrics)
Lyric.play_lyric()
print('-'*20)
Lyric.find_lyric(8)
结果:
蓝莲花
没有什么能够阻挡
你对⾃由地向往
天⻢⾏空的⽣涯
你对⾃由地向往
--------------------
天⻢⾏空的⽣涯