1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self, tires, color, weight):
self._tires = tires
self._color = color
self._weight = weight
self.speed = 0
@property
def color(self):
return self.color
@color.setter
def color(self, value):
self._color = value
@property
def tires(self):
return self.tires
@tires.setter
def tires(self, value):
raise ValueError
@property
def weight(self):
return self.weight
@weight.setter
def weight(self, value):
raise ValueError
def park(self):
self.speed = 0
print('汽车变为停止状态!')
def speed_up(self):
if self.speed + 20 > 200:
print('已经是最高速度!')
return
self.speed += 20
print('速度加至%.1f迈' %self.speed)
def slow_down(self):
if self.speed - 20 <= 0:
print('速度减至停止状态!')
self.speed = 0
else:
self.speed -= 20
print('速度减为%.1f迈' %self.speed)
def info(self):
print('汽车轮胎数为%d,颜色为%s,重量为%.1f吨,现速度为%.1f迈'%(self._tires,self._color,self._weight,self.speed))
auto_car = Auto(4, '黑色', 1.3)
auto_car.info()
test1 = type(auto_car)(6, '黄色', 2.3)
test1.info()
test2 = auto_car.__class__(4, '灰色',1.6)
test2.info()
class CarAuto(Auto):
def __init__(self, tires, color, weight,air_condition = 'off', cd = 'off'):
super().__init__(tires, color, weight)
self._air_condition = air_condition
self._cd = cd
self.speed = 0
@property
def air_conditon(self):
if self._air_condition == 'on':
return '空调打开!'
elif self._air_condition == 'off':
return '空调关闭!'
@air_conditon.setter
def air_condition(self, value):
self._air_condition = value
@property
def cd(self):
if self._cd == 'on':
return 'cd打开!'
else:
return 'cd关闭!'
@cd.setter
def cd(self, value):
self._cd = value
def speed_up(self):
if self.speed + 10 > 100:
print('已经是最高速度!')
return
self.speed += 10
print('速度加至%.1f迈' % self.speed)
def slow_down(self):
if self.speed - 10 <= 0:
print('速度减至停止状态!')
self.speed = 0
else:
self.speed -= 10
print('速度减为%.1f迈' % self.speed)
def info(self):
print('汽车轮胎数为%d,颜色为%s,重量为%.1f吨,现速度为%.1f迈,空调处于%s状态,cd处于%s状态'\
% (self._tires, self._color, self._weight, self.speed, self._air_condition, self._cd))
car = CarAuto(4, '黄色', 1.2, 'on', 'on')
car.info()
2.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
num = 0
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
Person.num += 1
Person.count()
def eat(self, food):
print('%s在吃%s'%(self.name, food))
@classmethod
def count(cls):
print('现在已经创建了%d个对象!'%Person.num)
a = Person('小明',12,'男')
b = Person('小花',13,'女')
3.创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self, gender, age, color, type):
self._gender = gender
self.age = age
self.color = color
self._type = type
@property
def gender(self):
return self._gender
@gender.setter
def gender(self, value):
raise ValueError
@property
def type(self):
return self.type
@type.setter
def type(self, value):
raise ValueError
def print_info(self):
print('/%s的对象:性别-%s 年龄-%d 颜色-%s 类型-%s'%(self.__class__.__name__,self._gender,\
self.age,self.color,self._type))
animal = Animal('雄性',4,'黑色','哺乳类')
animal.print_info()
4.写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。
但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class WriteError(Exception):
def __str__(self):
return '属性不能修改值'
class Circle:
pi = 3.1415926
def __init__(self, radius):
self.radius = radius
self._area = 0
self._perimeter = 0
@property
def area(self):
return Circle.pi * (self.radius ** 2)
@area.setter
def area(self, value):
raise WriteError
@property
def perimeter(self):
return 2 * Circle.pi * self.radius
@perimeter.setter
def perimeter(self, value):
raise WriteError
circle = Circle(3)
print('%.2f,%.2f'%(circle.area,circle.perimeter))
circle.area = 10
print(circle.area)
5.写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random
import operator
class Poker:
def __init__(self, color, count):
self.color = color
self.count = count
class Pokers:
color_list = ['红桃','黑桃','方块','梅花']
count_list = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
def __init__(self):
self.poker_list = []
self.framer1 = []
self.framer2 = []
self.landowner = []
self.hole_cards = []
self.landowner_num = 0
self.framer1_num = 0
self.framer2_num = 0
for color in Pokers.color_list:
for count in Pokers.count_list:
self.poker_list.append(Poker(color, count))
self.poker_list.append(Poker('王','大'))
self.poker_list.append(Poker('王','小'))
def poker_shuffle(self):
random.shuffle(self.poker_list)
def sort_poker(self, sort_list):
cmp_list = ['3','4','5','6','7','8','9','10','J','Q','K','A','2','小','大']
for i in cmp_list:
for j in sort_list[:]:
if j.count == i:
temp = j
sort_list.remove(j)
sort_list.append(temp)
def deal(self):
Pokers.poker_shuffle(self)
pokers_iter = iter(self.poker_list)
i = 1
for _ in range(51):
if i == 1:
self.landowner.append(next(pokers_iter))
self.landowner_num += 1
elif i == 2:
self.framer1.append(next(pokers_iter))
self.framer1_num += 1
elif i == 3:
self.framer2.append(next(pokers_iter))
self.framer2_num += 1
i = 0
i += 1
self.hole_cards = list(pokers_iter)
self.landowner.extend(self.hole_cards[:])
self.landowner_num += 3
def show(self):
print('\n==============底牌==============')
self.sort_poker(self.hole_cards)
for poker in self.hole_cards:
print(poker.__dict__)
print('\n==============地主牌==============')
self.sort_poker(self.landowner)
for poker in self.landowner:
print(poker.__dict__)
print('当前牌数:%d'%self.landowner_num)
print('\n==============农民一牌==============')
self.sort_poker(self.framer1)
for poker in self.framer1:
print(poker.__dict__)
print('当前牌数:%d'%self.framer1_num)
print('\n==============农民二牌==============')
self.sort_poker(self.framer2)
for poker in self.framer2:
print(poker.__dict__)
print('当前牌数:%d'%self.framer2_num)
a = Pokers()
a.deal()
a.show()
6.写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式
进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词
import operator
import time
class Lyric:
def __init__(self, time, lrc):
self.time = time
self.lrc = lrc
class Song:
def __init__(self):
self.song = []
self.time1 = 0
def load_lyric(self):
with open('files\lyric','r',encoding='utf-8')as f:
for line in f:
time = []
splite1 = line.strip().split(']')
for _ in splite1:
if _.startswith('['):
time.append(_.replace('[',''))
else:
lyc = _
for _ in time:
self.song.append(Lyric(_, lyc))
def sort_lyc(self):
cmp = operator.attrgetter('time')
self.song.sort(key = cmp)
def output(self):
timestamp1 = time.time()
time_tuple = time.localtime(57599)
while True:
s = time.asctime(time_tuple)
print(s[11:19],end='')
time.sleep(1)
print('\r', end='', flush=True)
timestamp = time.time()
time_tuple = time.localtime(timestamp - timestamp1 + 57599)
for i in range(len(self.song)):
tf = time.strftime("%M:%S", time_tuple)
if tf == self.song[i].time[:5]:
if self.song[i].lrc != '':
print(self.song[i].lrc)
def start(self):
self.load_lyric()
self.sort_lyc()
self.output()
s = Song()
s.start()
#files\lyric
[00:02.00]周杰伦 - 稻香
[00:04.00]词:周杰伦 曲:周杰伦
[00:06.00]
[00:08.00]www.90lrc.cn 欢迎您的光临!
[00:10.00]
[00:30.86]对这个世界如
[00:32.50]果你有太多的抱怨
[00:34.38]跌倒了
[00:35.48]就不敢继续往前走
[00:37.30]为什么
[00:38.45]人要这么的脆弱 堕落
[00:41.31]请你打开电视看看
[00:43.16]多少人 为生命在努力
[00:45.45]勇敢的走下去
[00:47.17]我们是不是该知足
[00:49.53]珍惜一切 就算没有拥有
[00:53.32]
[00:54.02]还记得你说家是唯一的城堡
[00:57.69]随着稻香河流继续奔跑
[01:00.63]微微笑 小时候的梦我知道
[01:05.81]不要哭让萤火虫带著你逃跑
[01:09.42]乡间的歌谣永远的依靠
[01:12.34]回家吧 回到最初的美好
[01:17.65]
[01:41.02]不要这么容易就想放弃
[01:43.35]就像我说的
[01:44.77]追不到的梦想
[01:45.75]换个梦不就得了
[01:47.86]为自己的人生鲜艳上色
[01:49.92]先把爱涂上喜欢的颜色
[01:51.98]
[01:52.68]笑一个吧
[01:53.81]功成名就不是目的
[01:55.72]让自己快乐快乐
[01:57.01]这才叫做意义
[01:58.62]童年的纸飞机
[02:00.12]现在终于飞回我手里
[02:03.67]
[02:04.37]所谓的那快乐
[02:05.71]赤脚在田里追蜻蜓追到累了
[02:08.62]偷摘水果被蜜蜂给叮到怕了
[02:11.75]谁在偷笑呢
[02:13.29]我靠着稻草人 吹着风
[02:15.27]唱着歌 睡着了
[02:16.81]哦 哦~
[02:17.57]午后吉它在虫鸣中更清脆
[02:19.65]哦 哦~
[02:20.50]阳光洒在路上就不怕心碎
[02:23.00]珍惜一切 就算没有拥有
[02:26.96]
[02:27.66]还记得你说家是唯一的城堡
[02:31.36]随着稻香河流继续奔跑
[02:34.25]微微笑 小时候的梦我知道
[02:39.40]不要哭让萤火虫带著你逃跑
[02:43.06]乡间的歌谣永远的依靠
[02:46.08]回家吧 回到最初的美好
[02:50.38]
[02:51.08]还记得你说家是唯一的城堡
[02:54.73]随着稻香河流继续奔跑
[02:57.73]微微笑 小时候的梦我知道
[03:02.76]不要哭让萤火虫带著你逃跑
[03:06.50]乡间的歌谣永远的依靠
[03:09.46]回家吧 回到最初的美好
[03:15.31]~~End~~