- 建立一個(gè)汽車類Auto论熙,包括輪胎個(gè)數(shù)福青,汽車顏色,車身重量脓诡,速度等屬性无午,并通過不同的構(gòu)造方法創(chuàng)建實(shí)例。至少要求
汽車能夠加速
減速
停車祝谚。 再定義一個(gè)小汽車類CarAuto
繼承Auto
并添加空調(diào)宪迟、CD屬性,并且重新實(shí)現(xiàn)方法覆蓋加速交惯、減速的方法
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('高級空調(diào)', '動感CD')
print(ao.speed_up())
print(ao.speed_down())
2.創(chuàng)建一個(gè)Person類次泽,添加一個(gè)類字段用來統(tǒng)計(jì)Perosn類的對象的個(gè)數(shù)
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.創(chuàng)建一個(gè)動物類,擁有屬性:性別席爽、年齡意荤、顏色、類型 只锻,
要求打印這個(gè)類的對象的時(shí)候以
'/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.寫一個(gè)圓類玖像, 擁有屬性半徑、面積和周長齐饮;要求獲取面積和周長的時(shí)候的時(shí)候可以根據(jù)半徑的值把對應(yīng)的值取到捐寥。
但是給面積和周長賦值的時(shí)候,程序直接崩潰沈矿,并且提示改屬性不能賦值
from math import pi
class ModifyError(Exception):
"""改錯(cuò)誤的類"""
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.(嘗試)寫一個(gè)類上真,其功能是:1.解析指定的歌詞文件的內(nèi)容
2.按時(shí)間顯示歌詞
提示:歌詞文件的內(nèi)容一般是按下面的格式進(jìn)行存儲的咬腋。歌詞前面對應(yīng)的是時(shí)間羹膳,在對應(yīng)的時(shí)間點(diǎn)可以顯示對應(yīng)的歌詞
[00: 00.20]藍(lán)蓮花
[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]當(dāng)你低頭地瞬間
[02: 26.79][02: 05.72][00: 37.16]才發(fā)覺腳下的路
[02: 32.17][00: 42.69]心中那自由地世界
[02: 37.20][00: 47.58]如此的清澈高遠(yuǎn)
[02: 42.32][00: 52.72]盛開著永不凋零
[02: 47.83][00: 57.47]藍(lán)蓮花
class ReadLyric:
"""讀取歌詞文件的類。功能還不完善"""
def __init__(self, time):
# 這個(gè)時(shí)間是你要讀取歌曲時(shí)間在幾分鐘之內(nèi)
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)
# 計(jì)算每句課次的時(shí)間點(diǎn)根竿,只能轉(zhuǎn)換成整型進(jìn)行比較陵像,因?yàn)楦↑c(diǎn)型在計(jì)算機(jī)里面精度會改變
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('這個(gè)時(shí)候時(shí)間點(diǎn)為:%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('有錯(cuò)誤')
# 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.解析歌詞文件中的內(nèi)容
with open('files/%s.txt' % self.name, encoding='utf-8') as f:
while True:
# 讀一行內(nèi)容
line = f.readline()
# 切割字符串
lines = line.split(']') # ['[02:11.27', '[01:50.22', '[00:21.95', '穿過幽暗地歲月\n']
# 遍歷時(shí)間創(chuàng)建歌詞對象
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.對當(dāng)前歌的歌詞按時(shí)間排序
self.__all_lyric.sort(reverse=True, key=lambda item: item.time)
print(self.__all_lyric)
def get_world(self, time):
# 解析歌詞文件
self.__analysis_file()
# 3.根據(jù)時(shí)間找對應(yīng)的歌詞
for item in self.__all_lyric:
if item.time < time:
return item.word
return '歌名'+self.name
l1 = LyricAnalysis('藍(lán)蓮花')
print(l1.get_world(30))
print(l1.get_world(10))
print(l1.get_world(10))
l1.name = '東風(fēng)破'
print(l1.get_world(20))
print(l1.get_world(30))
print(l1.get_world(10))
print(l1.get_world(10))
藍(lán)蓮花.txt文件內(nèi)容
[00:00.20]藍(lán)蓮花
[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]當(dāng)你低頭地瞬間
[02:26.79][02:05.72][00:37.16]才發(fā)覺腳下的路
[02:32.17][00:42.69]心中那自由地世界
[02:37.20][00:47.58]如此的清澈高遠(yuǎn)
[02:42.32][00:52.72]盛開著永不凋零
[02:47.83][00:57.47]藍(lán)蓮花