- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self, num_tires, color, weight, speed=0):
self.num_tires = num_tires
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self, num):
if num >= 0:
self.speed += num
else:
raise ValueError
def speed_down(self, num):
if 0 <= num < self.speed:
self.speed -= num
else:
raise ValueError
def park(self):
self.speed = 0
class CarAuto(Auto):
def __init__(self, num_tires, color, weight, air_condition, cd, speed=0):
super().__init__(num_tires, color, weight, speed=0)
self.air_condition = air_condition
self.cd = cd
def speed_up(self, num):
if num >= 0:
self.speed += num
if self.speed >= 200:
print('您已超速!')
else:
raise ValueError
def speed_down(self, num):
if 0 <= num < self.speed:
self.speed -= num
else:
raise ValueError
my_car = Auto(4, '红色', '2吨')
another_car = my_car.__class__(4, '白色', '2.8吨')
car3 = CarAuto(4, '白色', '2.8吨', '有空调', '无cd')
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
Person.count += 1
@classmethod
def look_over(cls):
print(cls.count)
p1 = Person('小明', 14, '男')
Person.look_over()
- 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self, gender, age, color, category):
self.gender = gender
self.age = age
self.color = color
self.category = category
def __repr__(self):
return '/{}的对象: 性别-{} 年龄-{} 颜色-{} 类型-{}/'.format(str(self.__class__.__name__), \
str(self.gender), str(self.age), str(self.color),
str(self.category))
cat = Animal('公', 2, 'white', '猫')
print(cat)
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
from math import *
class WriteError(Exception):
def __str__(self):
return '该属性不能赋值!'
class Circle:
def __init__(self, radius):
self.radius = radius
self._area = pi * radius**2
self._perimeter = 2*pi*radius
@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
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random
class Poker:
# 新牌
@staticmethod
def new_poker():
color_cards = ['\u2665', '\u2660', '\u2666', '\u2663']
normal_cards = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
pokers = ['king', 'joker']
for color in color_cards:
for x in normal_cards:
pokers.append(color + x)
return pokers
# 洗牌
@staticmethod
def shuffle(pokers):
random.shuffle(pokers)
# 发牌
@staticmethod
def deal(pokers):
landlord = iter(pokers)
play1, play2, play3 = [], [], []
num = 1
for _ in range(51):
if num == 1:
play1.append(next(landlord))
if num == 2:
play2.append(next(landlord))
if num == 3:
play3.append(next(landlord))
num = 0
num += 1
return play1, play2, play3, list(landlord)
poker = Poker.new_poker()
Poker.shuffle(poker)
print(poker)
print(Poker.deal(poker))
- (尝试)写一个类,其功能是: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
def find_str(str1: str, str2: str):
lists = []
for i in range(len(str1)):
if str1[i] == str2:
lists.append(i)
return lists
class ReadSong:
def __init__(self, path):
self.path = path
def analysis(self):
with open(self.path, 'r', encoding='utf-8') as song:
return song.readlines()
def show_song(self):
list1 = []
list_s = self.analysis()
for x in list_s:
i = find_str(x, ']')
songs = x[i[-1]+1:]
for y in range(len(i)):
if y == 0:
list1.append([x[:i[0]+1], songs])
continue
list1.append([x[i[y-1]+1:i[y]+1], songs])
list_time = []
for t in list1:
list_t = t[0][1:-1].split(':')
list_time.append([float(list_t[0])*60+float(list_t[1]), t[1]])
list_time.sort()
return list_time
def play(self):
list_p = self.show_song()
for i in range(len(list_p)):
if i == 0:
time.sleep(list_p[i][0])
print(list_p[i][1], end='')
continue
time.sleep(list_p[i][0]-list_p[i-1][0])
print(list_p[i][1], end='')
floor = ReadSong('words_ong.txt')
floor.play()