- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
"""______lxh______"""
class Auto: # init方法
def __init__(self, color, weight, speed, tyr_num):
self.tyr_num = tyr_num
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self):
self.speed += 3
print('速度增加了3码! Speed:', self.speed)
def speed_down(self):
if self.speed >= 3:
self.speed -= 3
print('减速了3码! Speed:', self.speed)
elif self.speed:
self.speed = 0
print('车速太低, 车停了!')
else:
print('汽车尚未启动!')
def park(self):
if self.speed:
while True:
if self.speed >= 8:
self.speed -= 8
print('正在刹车, Speed:', self.speed)
else:
self.speed = 0
print('车停了!')
break
else:
print('车已经停了!')
class Auto1: # 构造函数
tyr_num = 4
color = 'black'
weight = '1t'
speed = 35
def speed_up(self):
self.speed += 3
print('速度增加了3码! Speed:', self.speed)
def speed_down(self):
if self.speed >= 3:
self.speed -= 3
print('减速了3码! Speed:', self.speed)
elif self.speed:
self.speed = 0
print('车速太低, 车停了!')
elif self.speed:
self.speed = 0
else:
print('汽车尚未启动!')
def park(self):
if self.speed:
while True:
if self.speed >= 8:
self.speed -= 8
print('正在刹车, Speed:', self.speed)
else:
self.speed = 0
print('车停了!')
break
else:
print('车已经停了!')
class CarAuto(Auto): # 继承
def __init__(self, color, weight, speed, tyr_num, condition, cd):
super().__init__(color, weight, speed, tyr_num)
self.condition = condition
self.cd = cd
self.tyr_num = 4
def speed_up(self):
self.speed += 5
print('速度增加了5码! Speed:', self.speed)
def speed_down(self):
if self.speed >= 5:
self.speed -= 5
print('减速了5码! Speed:', self.speed)
elif self.speed:
self.speed = 0
print('车速太低, 车停了!')
elif self.speed:
self.speed = 0
def play_cond(self):
if self.condition == 'on':
self.condition = 'off'
print('关闭空调!')
else:
self.condition = 'on'
print('打开空调!')
def play_cd(self):
if self.cd == 'on':
self.cd = 'off'
print('关闭音乐!')
else:
self.cd = 'on'
print('打开音乐!')
car1 = Auto('black', '1t', 30, 4)
car2 = Auto1()
car1.speed_up()
car1.speed_down()
car1.park()
print(car2.color, car2.speed, car2.weight, car2.tyr_num)
car2.speed_up()
car2.speed_down()
car2.park()
car3 = CarAuto('black', '1t', 39, 4, 'on', 'on')
print(car3.__dict__)
car3.speed_up()
car3.speed_down()
car3.play_cd()
car3.play_cd()
car3.play_cond()
car3.play_cond()
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
"""______lxh______"""
class Person:
_num = 0
def __init__(self, name, age, sex):
self.name = name
self.age = age
self._sex = sex
Person._num += 1
print('有%d个对象!' % Person._num)
def show(self):
print(self.name, self.age, self._sex)
@property
def sex(self):
print('性别被访问!')
return self.sex
@sex.setter
def sex(self, value):
if value:
raise Readonly
class Readonly(Exception):
def __str__(self):
return 'Readonly'
p1 = Person('小明', 20, '男')
p2 = Person('小明', 20, '男')
p3 = Person('小明', 20, '男')
-
创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
"""______lxh______"""
class Animal:
def __init__(self, sex, age, color, breed):
self.sex = sex
self.age = age
self.color = color
self.breed = breed
def __str__(self):
return '/ ' + a1.__class__.__name__ + '的对象: 性别-' + self.sex + ', 年龄-' + str(self.age) + ', 颜色-' + self.color + ', 类型-' + self.breed + ' /'
a1 = Animal('母', 3, 'yellow', '哈奇士')
print(a1)
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
"""______lxh______"""
class Circle:
pi = 3.14
def __init__(self, radius: int):
self.radius = radius
self._area = self.pi * radius ** 2
self._girth = 2 * self.pi * self.radius
@property
def area(self):
return self._area
@area.setter
def area(self, value):
if value:
raise Readonly
@property
def girth(self):
return self._girth
@girth.setter
def girth(self, value):
if value:
raise Readonly
class Readonly(Exception):
def __str__(self):
return 'Readonly123'
c1 = Circle(5)
print(c1.area)
print(c1.girth)
# c1.area = 20
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
"""______lxh______"""
from random import shuffle
class Poker:
_num = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
_color = ['红桃', '黑桃', '方块', '梅花']
_Joker = 'JOKER'
_joker = 'joker'
_pokers = []
for n in _color:
for c in _num:
_pokers.append(c + n)
_pokers.append(_Joker)
_pokers.append(_joker)
shuffle(_pokers)
@classmethod
def deal(cls):
user1 = cls._pokers[:17]
user2 = cls._pokers[17:34]
user3 = cls._pokers[34:51]
print('User1', user1)
print('User2', user2)
print('User3', user3)
print('底牌:', cls._pokers[51:])
@classmethod
def riffle(cls):
shuffle(cls._pokers)
print(cls._pokers)
def main():
p1 = Poker
p1.deal()
p1.riffle()
p1.deal()
if __name__ == '__main__':
main()
- (尝试)写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词
"""______lxh______"""
from time import time, sleep
import pygame
class Music:
def __init__(self, lrc):
self._lrc = lrc/
self._time = 0
self._old_time = 0
def auto_lrc(self): # 自动播放音乐打印歌词
pygame.mixer.init()
pygame.mixer.music.load('files/Way Back Into Love.mp3') # 加载音乐
pygame.mixer.music.play() # 播放音乐
self._time = time() # 获得开始播放的时间戳
for tim in self._lrc:
while round(time() - self._time, 2) < tim: # 根据播放时间显示歌词
continue
else:
print(self._lrc[tim])
sleep(12) # 最后一句还有一段时间
pygame.mixer.music.stop() # 停止播放音乐
def show_lrc(self): # 打印指定时间的歌词
second = input('请输入时间(秒):')
sec = round(float(second), 2)
for tim in self._lrc:
if self._old_time < sec < tim: # 判断时间区间打印歌词
print(self._lrc[self._old_time])
self._old_time = 0
return
self._old_time = tim
def main():
lrc = {}
path = 'files/Way.lrc'
style = 'GBK'
with open(path, encoding=style) as f:
content = f.read()
content = content.splitlines()
content = [x for x in content if x != '']
for lin in content:
if lin[3] != ':' or lin[6] != '.': # 判断是否是说明文档
continue
li = lin.split(']')
for t in li[:-1]:
tim = t.split(':') # 分割时间
lrc[round(float(tim[0][1:]) * 60 + float(tim[1]), 2)] = li[-1] # round(num, 2) 保留小数点后两位 存秒数
lrc = dict(sorted(lrc.items(), key=lambda k: k[0]))
play = Music(lrc) # 创建对象
play.auto_lrc() # 播放音乐
# play.show_lrc() # 打印指定歌词
if __name__ == '__main__':
main()