1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
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" % self.speed)
else:
print('已经加速到最大值,不能加速了!')
def slow(self):
new_speed = self.speed - 20
if new_speed >= 0:
self.speed = new_speed
print("已减速,当前速度为%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("速度已经提升至%d" % self.speed)
def slow(self):
self.speed -= 40
print("速度已经降至%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.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self):
Person.count += 1
3.创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/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.写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
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.解析指定的歌词文件的内容 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 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))