0.定义一个学生类。有属性:姓名、年龄、成绩(语文,数学,英语)[每课成绩的类型为整数]
方法: a. 获取学生的姓名:getname() b. 获取学生的年龄:getage()
c. 返回3门科目中最高的分数。get_course()
class Students:
def __init__(self,name,age,scors):
self.name = name
self.age = age
self._scors = scors
def getname(self):
return self.name
def getage(self):
return self.age
def get_course(self):
max1 = self._scors['语文']
for score in self._scors:
if self._scors[score] > max1:
max1 = self._scors[score]
return max1
# return max(self.scors,key=lambda score:scors[scors])
@property
def scors(self):
return self._scors
@scors.setter
def scors(self,value):
for x in value:
if not ValueError(value[x],int):
self._scors = value
s1 = Students('sea',22,{})
s1.scors.update({'语文':88,'数学':98,'英语':78})
print(s1.scors)
print(s1.get_course())
1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等成员变量,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD等成员变量 覆盖加速 减速的方法
class Auto:
def __init__(self,tires_num,car_color,car_weight,speed):
self.tires_num = tires_num
self.car_color = car_color
self.car_weight = car_weight
self.speed = speed
def add_speed(self,spend):
return self.speed + spend
def slow_speed(self,spend):
return self.speed - spend
def park_car(self):
return self.speed - self.speed
c1 = Auto(4,'black','600kg',100)
c2 = Auto(6,'white','800kg',80)
print(c1.park_car())
class CarAuto(Auto):
def __init__(self,tires_num,car_color,car_weight,speed,air_conditioning,CD):
Auto.__init__(self,tires_num,car_color,car_weight,speed)
self.air_conditioning = air_conditioning
self.CD = CD
def add_speed(self,spend):
return self.speed + spend + 2
def slow_speed(self,spend):
return self.speed - spend - 2
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,firstname,lastname,age,sex):
self.firstname = firstname
self.lastname = lastname
self.age = age
self.sex = sex
def describeuser(self):
print('%s最开始用名%s,年龄%d,性别%s' % (self.lastname,self.firstname,self.age,self.sex))
def greetuser(self):
print('尊敬的%s你好,很高兴为你服务' % self.lastname)
class Admin(User):
def __init__(self,firstname,lastname,age,sex,privileges):
User.__init__(self,firstname,lastname,age,sex)
self.privileges = privileges
def show_privileges(self):
print(self.lastname +' ' + self.privileges[0])
print(self.lastname + ' ' + self.privileges[1])
print(self.lastname + ' ' + self.privileges[2])
a1 = Admin('sea','dragon',22,'男',["can add post","can delete post","can ban user"])
a1.show_privileges()
3.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self,name,age,sex,n_id):
Person.count += 1
self.name = name
self.age = age
self.sex = sex
self.n_id = n_id
p1 = Person('sea',22,'男','001')
p2 = Person('dreagon',21,'男','002')
print(Person.count)
'''
(尝试)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]蓝莲花
'''
'''
(尝试)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]蓝莲花
'''
class Lyrics:
def __init__(self,time,lyrics):
self.time = time
self.lyrics = lyrics
def __str__(self):
return '%.2f %s' % (self.time, self.lyrics)
def conversion_time(self):
new_time = []
if self.time:
for t in self.time:
num1 = t.index(':')
minutes = int(t[1:num1])*60
seconds = float(t[num1+1:])
time = minutes + seconds
new_time.append(time)
return new_time
class Analysis_lyrics():
def __init__(self,):
self.content = ' '
self.song = []
with open('./files/lyrics.txt','r',encoding='utf-8') as f1:
content = f1.read()
self.__get_lyrics(content)
def __get_lyrics(self,content):
cut_song = content.splitlines()
for song in cut_song:
cut_time_lyrics = song.split(']')
cut_time_lyrics = list(cut_time_lyrics)
for t in cut_time_lyrics[0,-1]:
num1 = t.index(':')
minutes = int(t[1:num1])*60
seconds = float(t[num1+1:])
time = minutes + seconds
song = Lyrics(time,cut_time_lyrics[-1])
self.song.append(song)
老师正确答案
"""__author__ = 余婷"""
class Lyric:
def __init__(self, time, word):
self.time = time
self.word = word
def __str__(self):
return '%.2f %s' % (self.time, self.word)
def __gt__(self, other):
return self.time > other.time
class LyricAnalysis:
"""歌词解析类"""
# 创建解析器对象的时候告诉我这个解析器是要解析哪首歌
def __init__(self, song_name):
# 保证一个歌词解析器对象对应一首歌
self.song_name = song_name
# 一首歌对应一个容器
self.all_lyric = []
# 解析歌词
self.__collect_lyric()
def __get_time_word(self, content):
"""提取歌词和时间"""
contents = content.split(']')
# 词
word = contents[-1]
for time in contents[:-1]:
# 将时间转换秒
times = time[1:].split(':')
fen = float(times[0])
miao = float(times[1])
new_time = fen*60 + miao
# 根据时间和词创建歌词对象
lyric = Lyric(new_time, word)
# 保存歌词对象
self.all_lyric.append(lyric)
def __collect_lyric(self):
"""将时间和词提取出来"""
# 读歌词文件中的内容
try:
with open('./files/%s.txt' % self.song_name, 'r', encoding='utf-8') as f:
# 一行一行的读
line = f.readline()
while line:
# 将每一行中的内容的词和时间弄出来
self.__get_time_word(line)
line = f.readline()
# 排序
self.all_lyric.sort(reverse=True)
# for lyric in self.all_lyric:
# print(lyric)
except FileNotFoundError:
print('文件不存在')
def get_word(self, time):
"""根据时间获取歌词"""
for lyric in self.all_lyric:
if lyric.time <= time:
return lyric.word
an1 = LyricAnalysis('蓝莲花')
print(an1.get_word(0.2))
# an2 = LyricAnalysis('十年')
if __name__ == '__main__':
pass
网友评论