- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self, tire, color, weight, price, speed=0):
self.tire = tire
self.color = color
self.weight = weight
self.speed = speed
self.price = price
def up_speed(self, speed_num: int):
self.speed += speed_num
return self.speed
def down_speed(self, speed_num: int):
self.speed -= speed_num
return self.speed
car1 = Auto(4, '黑色', 1, 100, 20)
car2 = Auto(4, '白色', 2, 50)
class CarAuto(Auto):
def __init__(self, air_conditon, music_cd):
super.__init__()
self.air_condition = air_conditon
self.music_cd = music_cd
def up_speed(self, speed_num: int):
self.speed += speed_num*2
return self.speed
def down_speed(self, speed_num: int):
self.speed -= speed_num*2
return self.speed
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self):
Person.count += 1
p1 = Person()
p2 = Person()
p3 = Person()
print(Person.count) # 3
- 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self, gender, age, color, a_type):
self.gender = gender
self.age = age
self.color = color
self.a_type = a_type
def __repr__(self):
return '/{}的对象:性别-{} 年龄-{} 颜色-{} 类型-{}/'. \
format('Animal', self.gender, self.age, self.color, self.a_type)
ani = Animal('公', 12, '黑色', '狗')
print(ani) #/Animal的对象:性别-公 年龄-12 颜色-黑色 类型-狗/
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class WriteError(Exception):
def __str__(self):
return '该属性不赋值'
class Circle:
def __init__(self, radius=0):
self.radius = radius
self._area = pi * self.radius ** 2
self._perimeter = 0
@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
def get_area(self, input_radius=0):
if input_radius:
radius = input_radius
else:
radius = self.radius
return pi * radius ** 2
def get_perimeter(self, input_radius=0):
if input_radius:
radius = input_radius
else:
radius = self.radius
return 2 * pi * radius
c1 = Circle(50)
print(c1.area)
print(c1.get_area())
print(c1.get_area(60))
# c1.area = 300 # __main__.WriteError: 该属性不赋值
print(c1.perimeter)
print(c1.get_perimeter())
print(c1.get_perimeter(20))
# c1.perimeter = 10 # __main__.WriteError: 该属性不赋值
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
class PokerCard:
poker_cards = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')
poker_colors = ('♠', '♥', '♣', '♦', 'JOKER')
def __init__(self, player_name=3):
self.player_name = player_name
self.poker_cards = 13
@staticmethod
def create_cards():
"""
:return:返回打乱顺序的54张牌
"""
numbers = [poker_size for poker_size in range(54)]
random.shuffle(numbers)
return numbers
@staticmethod
def init_cards(list_cards: list):
init_card = [(PokerCard.poker_cards[number % 13], PokerCard.poker_colors[number // 13])
for number in list_cards]
return init_card
def create_player_cards(self, numbers):
print(numbers, len(numbers))
# size = numbers%13
# color = numbers//13
player1 = self.init_cards(numbers[0:17])
player2 = self.init_cards(numbers[17:34])
player3 = self.init_cards(numbers[34:51])
landlord_cards = self.init_cards(numbers[51:54])
print('player1:', player1, len(player1))
print('player2:', player2, len(player2))
print('player3:', player3, len(player3))
print(landlord_cards)
def start_game(self):
numbers = self.create_cards()
self.create_player_cards(numbers)
def shuffle_card(self):
numbers = self.create_cards()
random.shuffle(numbers)
self.create_player_cards(numbers)
p1 = PokerCard()
p1.start_game()
p1.shuffle_card()
- (尝试)写一个类,其功能是: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]蓝莲花
import bisect
# lyric
# analysis
class AnalysisLyric:
lrc_dict = {}
def __init__(self, file_name, file_path):
self.file_name = file_name
self.file_path = file_path
def analysis_lyric_file(self):
with open(self.file_path + self.file_name, 'r', encoding='utf-8') as f:
lyric_str = f.readlines()
return lyric_str
def analysis_lyric(self):
lyric_str = self.analysis_lyric_file()
# 遍历所有元素,干掉方括号
for i in lyric_str:
# 取出方括号并切割歌词字符串
lrc_word = i.replace("[", "]").strip().split("]")
for j in range(len(lrc_word) - 1):
if lrc_word[j]:
# print(lrc_word[j], end=",")
AnalysisLyric.lrc_dict[lrc_word[j]] = lrc_word[-1]
# 遍历字典,对字典的key进行排序,
for key in sorted(AnalysisLyric.lrc_dict.keys()):
print(key, AnalysisLyric.lrc_dict[key])
@classmethod
def output_timeLrc(cls, time_str='00:00.00'):
lrc_dict_key = sorted(cls.lrc_dict.keys())
position = bisect.bisect_left(lrc_dict_key, time_str)
print(cls.lrc_dict[lrc_dict_key[position]])
analrc_1 = AnalysisLyric('1.lrc', './')
analrc_1.analysis_lyric()
AnalysisLyric.output_timeLrc('00:20.00')