- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求
汽车能够加速
减速
停车。 再定义一个小汽车类CarAuto
继承Auto
并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
"""汽车类Auto"""
def __init__(self, tyre=0, color='白色', weight=0, speed=0):
self.tyre = tyre
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self):
"""汽车的加速方法"""
self.speed += 10
return self.speed
def speed_down(self):
"""汽车的减速方法"""
self.speed = 0
return self.speed
def stop_car(self):
self.speed_down()
print('车停好了')
class CarAuto(Auto):
"""Auto子类"""
def __init__(self, air_condition: str, cd: str):
super().__init__()
self.air_condition = air_condition
self.cd = cd
def speed_up(self):
"""CarAuto的加速方法"""
self.speed += 12
return self.speed
def speed_down(self):
"""CarAuto的减速方法"""
self.speed = 0
return self.speed
ao = Auto(4, '白色', 400, 10)
carao = CarAuto('高级空调', '动感CD')
print(ao.speed_up())
print(ao.speed_down())
2.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
num = 0
def __init__(self):
Person.num += 1
p1 = Person()
print(Person.num)
p2 = Person()
print(Person.num)
p3 = Person()
print(Person.num)
3.创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以
'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/'
的形式来打印
class Animal:
"""动物类"""
def __init__(self, gender, age, color, type_):
self.gender = gender
self.age = age
self.color = color
self.type_ = type_
def animal_print(self):
"""打印动物的信息"""
print('%s的对象: 性别-%s 年龄-%d 颜色-%s 类型-%s' % (self.__class__, self.gender, self.age, self.color, self.type_))
animal = Animal('母', 18, '白色', '人妖')
animal.animal_print()
4.写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。
但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
from math import pi
class ModifyError(Exception):
"""改错误的类"""
def __str__(self):
return '尝试修改只读属性的值——Attempts to modify the value of the read-only property'
class Circle:
"""圆类"""
def __init__(self, radius):
self._radius = radius
self._perimeter = 2*pi*radius
self._area = pi*radius**2
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
self._radius = value
self._perimeter = 2*pi*value
self._area = pi*value**2
@property
def perimeter(self):
return self._perimeter
@perimeter.setter
def perimeter(self, value):
raise ModifyError
@property
def area(self):
return self._area
@area.setter
def area(self, value):
raise ModifyError
circle = Circle(3)
print(circle.perimeter)
print(circle.area)
# circle.perimeter = 1
circle.area = 1
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 ReadLyric:
"""读取歌词文件的类。功能还不完善"""
def __init__(self, time):
# 这个时间是你要读取歌曲时间在几分钟之内
self.time = time
def read(self):
with open('lyric.txt', encoding='utf-8') as f:
temp = True
time_add = 0
while temp:
time_temp = 0
content = f.readline()
temp1 = True
while temp1:
time_add += 1
# print(time_add)
# 计算每句课次的时间点,只能转换成整型进行比较,因为浮点型在计算机里面精度会改变
time_temp = (int(content[1:3]) * 60 * 100 + int(content[4:6]) * 100 + int(content[7:9]))
try:
if content[10] == '[':
time_temp += (int(content[11:13]) * 60 * 100 + int(content[14:16]) * 100 + int(content[17:19]))
elif content[20] == '[':
time_temp += (int(content[21:23]) * 60 * 100 + int(content[24:26]) * 100 + int(content[27:29]))
elif content[30] == '[':
time_temp += (int(content[31:33]) * 60 * 100 + int(content[34:36]) * 100 + int(content[37:39]))
elif content[40] == '[':
time_temp += (int(content[41:43]) * 60 * 100 + int(content[44:46]) * 100 + int(content[47:49]))
except IndexError:
pass
finally:
if time_temp == time_add:
print(content[10:])
# print('这个时候时间点为:%s' % content[:10])
temp1 = False
if time_add == self.time*60*100:
temp = False
# print(time_temp)
# print(content[10:])
# try:
#
# except:
# temp = False
# print('有错误')
# print(f.readline()[10:])
# print(f.readline()[:10])
read = ReadLyric(10)
read.read()
老师的版本
"""__author__ = 余婷"""
class Lyric:
def __init__(self, word):
self._time = 0
self.word = word
@property
def time(self):
return self._time
@time.setter
def time(self, value):
# value = '[00:45.99'
fen = float(value[1:3])
miao = float(value[4:])
self._time = fen*60 + miao
def __repr__(self):
return str(self.__dict__)
class LyricAnalysis:
def __init__(self, name):
self._name = name
self.__all_lyric = []
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
self.__all_lyric.clear()
def __analysis_file(self):
if not self.__all_lyric:
print('=========去解析歌词===============')
# 1.解析歌词文件中的内容
with open('files/%s.txt' % self.name, encoding='utf-8') as f:
while True:
# 读一行内容
line = f.readline()
# 切割字符串
lines = line.split(']') # ['[02:11.27', '[01:50.22', '[00:21.95', '穿过幽暗地岁月\n']
# 遍历时间创建歌词对象
for time_str in lines[:-1]:
lyric_obj = Lyric(lines[-1])
lyric_obj.time = time_str
# print(lyric_obj.__dict__)
self.__all_lyric.append(lyric_obj)
if not line:
break
# 2.对当前歌的歌词按时间排序
self.__all_lyric.sort(reverse=True, key=lambda item: item.time)
print(self.__all_lyric)
def get_world(self, time):
# 解析歌词文件
self.__analysis_file()
# 3.根据时间找对应的歌词
for item in self.__all_lyric:
if item.time < time:
return item.word
return '歌名'+self.name
l1 = LyricAnalysis('蓝莲花')
print(l1.get_world(30))
print(l1.get_world(10))
print(l1.get_world(10))
l1.name = '东风破'
print(l1.get_world(20))
print(l1.get_world(30))
print(l1.get_world(10))
print(l1.get_world(10))
蓝莲花.txt文件内容
[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]蓝莲花