- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self):
self.count = 4
self.color = '黑色'
self.weight = '2000kg'
self.speed = '80公里/h'
def speed_up(self):
print('加速!')
@classmethod
def speed_cut(cls):
print('减速!')
@staticmethod
def park():
print('减速!')
class CarAuto(Auto):
def __init__(self, air_conditioner, cd):
super().__init__()
self.air_conditioner = air_conditioner
self.cd = cd
def speed_up(self):
print('小汽车加速')
super().speed_up()
@classmethod
def speed_cut(cls):
print('小汽车减速')
super().speed_cut()
c1 = CarAuto('hr', 'VCD')
c1.speed_up()
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
sum1 = 0
def __init__(self):
Person.sum1 += 1
p1 = Person()
p2 = Person()
P3 = Person()
print(Person.sum1)
- 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self, gender, age, color, breed):
self.gender = gender
self.age = age
self.color = color
self.breed = breed
def __repr__(self):
return '/XXX的对象:性别-%s age-%s color-%s breed-%s' % \
(self.gender, self.age, self.color, self.breed)
a1 = Animal('雄性', 4, '黑色', '土狗')
print(a1.__repr__())
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class WriteError(Exception):
def __str__(self):
return '该属性不能赋值!'
class Circle:
pi = 3.1415926
def __init__(self, radius):
self.radius = radius
self._area = Circle.pi * self.radius ** 2
self._perimeter = Circle.pi * self.radius * 2
@property
def area(self):
self._area = Circle.pi * self.radius ** 2
return self._area
@area.setter
def area(self, value):
raise WriteError
@property
def perimeter(self):
self._perimeter = Circle.pi * self.radius * 2
return self._perimeter
@perimeter.setter
def perimeter(self, value):
raise WriteError
c1 = Circle(4)
print(c1.area)
print(c1.perimeter)
# c1.area = 4
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random
class Poker:
def __init__(self):
list1 = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
list2 = ['♥', '♠', '♣', '♦']
new_list = []
for x1 in list1:
for x2 in list2:
new_list.append(x1 + x2)
new_list += ['小王', '大王']
self.cards = new_list
def shuffle_poker(self):
random.shuffle(self.cards)
def deal_poker(self, n=3):
self.people = []
for _ in range(n):
self.people.append([])
for x in self.people:
for _ in self.cards[::n]:
x.append(_)
del self.cards[0]
for num in range(1, len(self.people) + 1):
print('第%s个人的牌是:%s' % (num, self.people[num-1]))
p1 = Poker() # 拿出一副新牌
p1.shuffle_poker() # 洗牌
p1.deal_poker(6) # 发牌
- (尝试)写一个类,其功能是: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 time
class Lyrics:
def __init__(self):
self.list_t = []
self.list_l = []
self.list_t_1 = []
self.list_l_1 = []
self.str_t = ''
self.str_l = ''
self.a = 0
def time_lyrics(self):
with open('蓝莲花', 'r', encoding='utf-8') as f:
while True:
list1 = list(f.readline())
if not list1:
break
for x in list1:
if '0' <= x <= '9':
self.list_t.append(x)
if '\u4E00' <= x <= '\u9FA5':
self.list_l.append(x)
for x in range(0, len(self.list_l)):
self.str_l += self.list_l[x]
self.list_l_1.append(self.str_l)
self.str_l = ''
for _ in range(0, len(self.list_t), 6):
for x in range(_, _ + 6):
self.str_t += self.list_t[x]
time1 = int(self.str_t)
time1 = float(time1 // 10000 * 60 + time1 %
10000 // 100 + time1 % 100 / 100)
self.list_t_1.append(time1)
self.str_t = ''
self.list_l = []
self.list_t = []
self.list_t_1.sort()
self.list_t_1.insert(0, 0)
for x in range(5, 9):
self.list_l_1.append(self.list_l_1[x])
for x in range(5, 13):
self.list_l_1.append(self.list_l_1[x])
# print(self.list_l_1)
# print(self.list_t_1)
def lyric_time(fn):
def test(*args, **kwargs):
time.sleep(self.list_t_1[self.a + 1] - l1.list_t_1[self.a])
fn(*args, **kwargs)
self.a += 1
return test
for char in self.list_l_1:
@lyric_time
def lyric_print():
print(char)
lyric_print()
l1 = Lyrics()
l1.time_lyrics()