- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self,color,weight):
self.number_of_tires=5
self.color=color
self._weight=weight
self.speed=0
def speed_up(self,value):
if not isinstance(value,int):
raise ValueError
if value<=0:
raise ValueError
return ('加速到%d'%(self.speed+value))
def speed_cut(self,value):
if not isinstance(value,int):
raise ValueError
if value<=0:
raise ValueError
return ('减速到%d'%(self.speed+value))
def stop(self):
self.speed=0
return self.speed
@property
def weight(self):
return self._weight
@weight.setter
def weight(self,value):
raise ValueError
class CarAuto(Auto):
def __init__(self,air_condition,cd):
super().__init__()
self.air_condition=air_condition
self.CD=cd
def speed_up(self,value):
print('我是carAuto')
super().speed_up(value)
def speed_cut(self,value):
print('我是carAuto')
super().speed_cut(value)
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
num = 0
def __init__(self):
Person.num+=1
pass
c1= Person()
c2=Person()
print(Person.num)
- 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self,gender,age,color,types):
self.gender=gender
self.age=age
self.color=color
self.types=types
def __str__(self):
str1=str(self.__class__)[17:-2]
return '%s的对象:'%str1+'性别-%s,'%self.gender+'年龄-%s,'%self.age+'颜色-%s,'%self.color+'类型-%s'%self.types
dog=Animal('公',10,'黄色','赌狗')
print(dog)
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class WriteError(Exception):
def __str__(self):
return '不能写,只能读'
from math import pi
class Circle:
def __init__(self,r):
self.r=r
self._area=0
self._perimeter=0
@property
def area(self):
return pi*self.r**2
@area.setter
def area(self,value):
raise WriteError
@property
def perimeter(self):
return pi*2*self.r
@perimeter.setter
def perimeter(self, value):
raise WriteError
c1=Circle(4)
print(c1.perimeter)
c1.perimeter=4
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random
class Poker:
num = [str(num1) for num1 in range(2, 11)]
num.extend('AJQK')
str1 = ['♥', '♠', '♣', '♦']
def __init__(self):
pass
@classmethod
def card(cls):
cards=list(('%s%s'%(str2,num2) for str2 in cls.str1 for num2 in cls.num))
cards.append('大王')
cards.append('小王')
return cards
@classmethod
def riffle(cls):
li=cls.card()
random.shuffle(li)
return li
def deal(self):
pokers=self.riffle()
p1=[]
p2=[]
p3=[]
n=0
while True:
n+=1
if len(pokers)==3:
break
if n==1:
p1.append(pokers.pop())
if n==2:
p2.append(pokers.pop())
if n==3:
p3.append(pokers.pop())
n%=3
hole_cards=pokers[:]
return (p1,p2,p3,hole_cards)
- (尝试)写一个类,其功能是: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 bisect
class DynamicLyrics:
def __init__(self,path:str):
self.path=path
def opne(self):
with open(self.path,'r',encoding='utf-8') as f:
cont=f.read()
return cont.split('\n')
def dict_g(self):
l2 = []
l3 = []
for itme in self.opne()[:-1]:
l1 = itme.split(']')
l2.append(l1)
for itme1 in l2:
if len(itme1) > 2:
for i in range(len(itme1) - 1):
l3.append({itme1[i]: itme1[-1]})
else:
l3.append({itme1[0]: itme1[1]})
return l3
def dict_time(self):
l1=self.dict_g()
l2=[]
for itme in l1:
for key in itme:
str1=key[1:]
list1=str1.split(':')
time = float(int(list1[0]) * 60 + float(list1[1]))
l2.append({'%.2f' % time: itme[key]})
return l2
def list_time(self):
l1=self.dict_time()
l2=[]
for itme in l1:
for key in itme:
l2.append(float(key))
l2.sort()
return l2
def query_the_lyrics(self,num:int):
l1=self.list_time()
l2=self.dict_time()
num1 = bisect.bisect(l1, num) - 1
num2= l1[num1]
num2= '%.2f' % num2
for itme in l2:
for key in itme:
if key == str(num2):
return itme[key]
d1=DynamicLyrics('geci.txt')
print(d1.query_the_lyrics(30))
#也曾感到彷徨