1.建立一個汽車類Auto,包括輪胎個數(shù)竞穷,汽車顏色,車身重量鳞溉,速度等屬性瘾带,并通過不同的構(gòu)造方法創(chuàng)建實例。至少要求 汽車能夠加速 減速 停車熟菲。 再定義一個小汽車類CarAuto 繼承Auto 并添加空調(diào)看政、CD屬性,并且重新實現(xiàn)方法覆蓋加速科盛、減速的方法
class Auto:
def __init__(self, tyre=4, color='white', weight=0, speed=0):
self.tyre = tyre
self.color = color
self.weight = weight
self.speed = speed
def accelerate(self):
speed_most = 150
new_speed = self.speed + 20
if new_speed <= speed_most:
self.speed = new_speed
print("已加速帽衙,當(dāng)前速度為%d" % self.speed)
else:
print('已經(jīng)加速到最大值,不能加速了贞绵!')
def slow(self):
new_speed = self.speed - 20
if new_speed >= 0:
self.speed = new_speed
print("已減速厉萝,當(dāng)前速度為%d" % self.speed)
else:
print('不能減速')
def stop(self):
self.speed = 0
print("已停車")
car1 = Auto()
car1.tyre = 4
car1.color = 'yellow'
car1.weight = '666'
car1.speed = 120
car1.slow()
car2 = Auto(4, 'black', 777, 140)
car2.accelerate()
class CarAuto(Auto):
def __init__(self):
super().__init__()
self.air_conditioned = False
self.CD = False
def accelerate(self):
self.speed += 30
print("速度已經(jīng)提升至%d" % self.speed)
def slow(self):
self.speed -= 40
print("速度已經(jīng)降至%d" % self.speed)
new_car1 = CarAuto()
new_car1.air_conditioned = True
new_car1.CD = True
new_car1.speed = 128
print(new_car1.air_conditioned)
new_car1.accelerate()
new_car1.slow()
2.創(chuàng)建一個Person類,添加一個類字段用來統(tǒng)計Perosn類的對象的個數(shù)
class Person:
count = 0
def __init__(self):
Person.count += 1
3.創(chuàng)建一個動物類榨崩,擁有屬性:性別谴垫、年齡、顏色母蛛、類型 翩剪,
要求打印這個類的對象的時候以'/XXX的對象: 性別-? 年齡-? 顏色-? 類型-?/' 的形式來打印
class Animal:
def __init__(self, gender='公', age=0, color='yellow', atype='dog'):
self.gender = gender
self.age = age
self.color = color
self.atype = atype
def __str__(self):
return str(self.__class__)+'的對象:性別-'+self.gender+',年齡-'+str(self.age)+',類型—'+self.atype
animal1 = Animal('母', 2, 'black', 'bird')
print(animal1)
4.寫一個圓類, 擁有屬性半徑彩郊、面積和周長前弯;要求獲取面積和周長的時候的時候可以根據(jù)半徑的值把對應(yīng)的值取到。但是給面積和周長賦值的時候秫逝,程序直接崩潰恕出,并且提示改屬性不能賦值
class WriteError(Exception):
def __str__(self):
return '改屬性不能賦值!'
class Circle:
def __init__(self, radius=0):
self._radius = radius
self._area = radius**2*math.pi
self._perimeter = radius*math.pi*2
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
self._radius = value
self._area = self._radius**2*math.pi
self._perimeter = self._radius*math.pi*2
@property
def area(self):
return self._area
@area.setter
def area(self, value):
raise WriteError
@property
def perimeter(self):
return self._perimeter
@perimeter.setter
def perimeter(self, value):
raise WriteError
c1 = Circle(2)
print(c1.perimeter)
c1.radius = 3
print(c1.perimeter)
5.(嘗試)寫一個類,其功能是:1.解析指定的歌詞文件的內(nèi)容 2.按時間顯示歌詞 提示:歌詞文件的內(nèi)容一般是按下面的格式進(jìn)行存儲的违帆。歌詞前面對應(yīng)的是時間浙巫,在對應(yīng)的時間點可以顯示對應(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 Song:
def __init__(self, path):
self.path = path
self.line = ''
self.time = []
def time_list(self):
time_lists = []
with open(self.path, encoding='utf-8') as f:
line = f.readline()
while line:
lyric_list = line.split(']')
for time_str in lyric_list[:-1]:
time_dict = {}
real_time = time_str.split('[')[1]
minute = int(real_time.split(':')[0])
second = float(real_time.split(':')[1])
all_second = minute*60+second
time_dict['time'] = all_second
time_dict['lyric'] = lyric_list[-1]
time_lists.append(time_dict)
line = f.readline()
time_lists.sort(key=lambda x: x['time'])
return time_lists
def analysis_lyric(self, time=0):
time_lists = self.time_list()
for time_dic in time_lists:
if time_dic['time'] < time:
text = time_dic['lyric']
else:
break
return text
s1 = Song('lyric.txt')
print(s1.analysis_lyric(160))