- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
wheel = 4
def __init__(self, color, weight, speed):
self.color = color
self.weight = weight
self.speed = speed
def accelerate(self, much):
self.speed += much
def slow_down(self, much):
self.speed -= much
if self.speed < 0:
print('已停车')
else:
print('减速')
def stop(self):
self.speed = 0
print('已停车')
class CarAuto(Auto):
def __init__(self, color, weight, speed, air_c, cd):
super().__init__(self.color, self.weight, self.speed)
self.air_c = air_c
self.cd = cd
def accelerate(self, much):
self.speed += much
print('已加速当前速度为:', self.speed)
def slow_down(self, much):
self.speed -= much
if self.speed < 0:
print('已减速,车辆速度不足,请停车!')
else:
print('已减速当前速度为:', self.speed)
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
num = 0
def __init__(self):
Person.num += 1
p1 = Person()
p2 = Person()
print(Person.num)
-
创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以’/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/’ 的形式来打印
class Animal:
def __init__(self, sex, age, color, species):
self.sex = sex
self.age = age
self.color = color
self.species = species
def __repr__(self):
return f'/{Animal.__name__}的对象: 性别:{self.sex} 年龄:{self.age} 颜色:{self.color} 类型:{self.species}/'
a1 = Animal('雌', 1, '绿', '泰迪')
print(a1)
- (不写)写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class Circle:
def __init__(self, radius, area, perimeter):
self.radius = radius
self.area = area
self.perimeter = perimeter
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
class Poker_card:
colors = ['♡', '♤', '♣', '♢']
nums = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'joker', 'JOKER']
table1 = {
'J': 11, 'Q': 12, 'K': 13, 'A': 14, '2': 15, 'joker': 16, 'JOKER': 17}
def __init__(self, color, num):
self.color = color
self.num = num
def __repr__(self):
return f'{self.color}{self.num}'
def get_value(self):
number = self.num
if '3' <= number <= '9' or number == '10':
return int(number)
return Poker_card.table1[number]
def __lt__(self, other):
return self.get_value() < other.get_value()
class Poker:
def __init__(self):
self.pokers = []
self.pokers = [Poker_card(x, y) for x in Poker_card.colors for y in Poker_card.nums[:-2]]
self.pokers.extend([Poker_card('', Poker_card.nums[-2]), Poker_card('', Poker_card.nums[-1])])
def shuffle(self):
shuffle(self.pokers)
def deal(self):
self.shuffle()
p1 = []
p2 = []
p3 = []
for _ in range(17):
p1.append(self.pokers.pop(0))
p2.append(self.pokers.pop(0))
p3.append(self.pokers.pop(0))
return sorted(p1, reverse=True), sorted(p2, reverse=True), sorted(p3, reverse=True), sorted(self.pokers, reverse=True)
game = Poker()
print(game.deal())
-
(尝试)写一个类,其功能是: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 Lyric: def __init__(self, time, word): self.time = time self.word = word def __lt__(self, other): return self.time < other.time class Analysis: def __init__(self): self.all_lyric = [] self.collect_lyric() def get_time_word(self, content): contents = content.split(']') word = contents[-1] # print(contents) for time in contents[:-1]: times = time[1:].split(':') fen = float(times[0]) second = float(times[1]) new_time = fen*60 + second lyric = Lyric(new_time, word) self.all_lyric.append(lyric) pass