- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self, wheel_count, color, weight, speed):
self.wheel_count = wheel_count
self.color = color
self.weight = weight
self.speed = speed
@staticmethod
def accelerate():
print('加速')
@staticmethod
def moderate():
print('减速')
@staticmethod
def stop():
print('停车')
class CarAuto(Auto):
def __init__(self, wheel_count, color, weight, speed, CD, aircondition):
super().__init__(wheel_count, color, weight, speed)
self.CD = CD
self.aircondition = aircondition
@staticmethod
def accelerate():
print('覆盖加速')
@staticmethod
def moderate():
print('覆盖减速')
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
num = 0
def __init__(self, name):
self.name = name
Person.num += 1
p1 = Person('xiaoming')
p2 = Person('xiaohua')
print(Person.num)
- 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self, gender, age, color, type):
self.gender = gender
self.age = age
self.color = color
self.type = type
def __repr__(self):
return '/{}的对象: 性别-{} 年龄-{} 颜色-{} 类型-{}/'.format(self.__class__.__name__,
self.gender, self.age, self.color, self.type)
a1 = Animal('male', 18, 'yellow', 'Mammals')
print(a1)
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
import math
class WriteError(Exception):
def __str__(self):
return '改属性不能赋值'
class Circle:
def __init__(self, r):
self.r = r
self._area = self.r ** 2 * math.pi
self._perimeter = 2 * math.pi * self.r
@property
def area(self):
return self._area
@property
def perimeter(self):
return self._perimeter
@area.setter
def area(self, value):
raise WriteError
@perimeter.setter
def perimeter(self, value):
raise WriteError
c1 = Circle(5)
print(c1.area, c1.perimeter)
c1.area = 18
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random
import time
class Person:
def __init__(self, name):
self.name = name
self.cards = []
class Pokers:
def __init__(self):
self.cards = []
# 生成
def generate_cards(self):
for x in '♥♣♦♠':
for y in range(2, 11):
self.cards.append(x + str(y))
for y in 'AJQK':
self.cards.append(x + y)
self.cards.append('Joker')
self.cards.append('joker')
# 洗
def shuffle(self):
random.shuffle(self.cards)
# 发
def send(self):
for x in iter(self.cards):
yield x
time.sleep(0.5)
if __name__ == '__main__':
p1 = Pokers()
p1.generate_cards()
p1.shuffle()
gen1 = p1.send()
pe1 = Person('小明')
pe2 = Person('小红')
pe3 = Person('小黄')
while True:
try:
for _ in range(len(p1.cards)):
x1 = next(gen1)
print(x1)
x2 = next(gen1)
print(x2)
x3 = next(gen1)
print(x3)
pe1.cards.append(x1)
pe2.cards.append(x2)
pe3.cards.append(x3)
print('{}的牌:{}'.format(pe1.name, ' '.join(pe1.cards)))
print('{}的牌:{}'.format(pe2.name, ' '.join(pe2.cards)))
print('{}的牌:{}'.format(pe3.name, ' '.join(pe3.cards)))
except StopIteration:
break
finally:
break
- (尝试)写一个类,其功能是: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 re
class Lyric:
def __init__(self):
self.words = ''
# self.minite = '0'
# self.second = '0'
class Lyricfile:
def __init__(self):
self.lyrics = []
@staticmethod
def open_file():
with open(r'./1.txt', 'r', encoding='utf-8') as fp:
lines = fp.readlines()
return lines
# 解析歌词内容
def analyze(self):
lines = self.open_file()
for line in lines:
str1 = line.replace('[', '')
str1 = str1.replace('\n', '')
str1 = re.split(r']', str1)
for str2 in str1[-2::-1]:
str2 = str2.split(':')
l1 = Lyric()
l1.words = str1[-1]
l1.minute = str2[0]
l1.second = str2[-1]
self.lyrics.append(l1)
def show(self):
self.lyrics.sort(key=lambda x: float(x.minute) * 60 + float(x.second))
for lyric in self.lyrics:
print(lyric.words)
def get_lyric(self, time):
time1 = time
time = time.split(':')
time_sum = float(time[0]) * 60 + float(time[1])
self.lyrics.sort(key=lambda lyric: float(lyric.minute) * 60 + float(lyric.second), reverse=True)
for x in self.lyrics:
if time_sum >= float(x.minute) * 60 + float(x.second):
return time1 + " " + x.words
return time1 + " 伴奏中..."
lf1 = Lyricfile()
lf1.analyze()
lf1.show()
print('==========我是分割线==========')
print(lf1.get_lyric('0:28.25'))
print(lf1.get_lyric('0:0.00'))
print(lf1.get_lyric('01:15.85'))
print(lf1.get_lyric('02:06.83'))