- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Car:
def __init__(self,wheel:int,color,weight,speed=5):
self.wheel =wheel
self.color = color
self.weight = weight
self.speed = speed
def add_speed(self):
return self.speed+1
def reduce_speed(self):
return self.speed-1
def stop(self):
if self.speed==0:
return '停车咯'
class CarAuto(Car):
def __init__(self,aircondition,CD):
super().__init__()
self.aircondition=aircondition
self.CD = CD
def add_speed(self):
return self.speed+2
def reduce_speed(self):
return self.speed-2
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
counts = 0
def __init__(self):
self.name = '1'
self.age ='2'
self.sex ='3'
Person.counts +=1
def counts1(self):
for i in self.__dict__:
Person.counts+=1
return Person.counts
a = Person()
print(a.counts1())
- 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal():
def __init__(self,sex,age,color,types):
self.sex = sex
self.age =age
self.color =color
self.types = types
def printf(self):
return str(self.__class__)[17:-2]+'的对象:'+' 性别:%s'%self.sex+' 年龄:%s'%self.age\
+' 颜色:%s'%self.color+' 类型:%s'%self.types
a = Animal('母','12','blue','柯基')
print(a.printf())
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class WirteError(Exception):
def __str__(self):
return '该属性不能赋值'
class Circle:
pi =3.141592653
def __init__(self,radius):
self._radius=radius
self._area = Circle.pi*radius**2
self._perimeter=2*Circle.pi*radius
@property
def radius(self):
return self._radius
@radius.setter
def radius(self,value):
if value>=0:
self._radius=value
return self._radius
else:
return ValueError
@property
def area(self):
return self._area
@area.setter
def area(self,value):
raise WirteError
@property
def perimeter(self):
return self._perimeter
@perimeter.setter
def perimeter(self, value):
raise WirteError
a = Circle(2)
print(a.area)
a.area = 12
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random
class Pooker:
def __init__(self):
str1 = [str(nums) for nums in range(2,11)]+list('JQKA')
str2='♠♣♥♦'
list1 = [i+'*'+m for i in str2 for m in str1]
list1.append('小王')
list1.append('大王')
self.pai =list1
@staticmethod
def xipai(self:list):
random.shuffle(self)
return self
def fapai(self):
list2 =[]
list1=[]
list3=[]
table=[]
table.append(list1)
table.append(list2)
table.append(list3)
Pooker.xipai(self.pai)
b = self.pai[52:]
for i in range(len(self.pai)-3):
table[i%3].append(self.pai[i])
a = random.randint(0,2)
table[a].extend(b)
return table
a = Pooker()
print(a.fapai())
- (尝试)写一个类,其功能是: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 Apply:
def __init__(self,name):
self.name = name
@staticmethod
def change(n):
sums = 0
a = n.split(':')
sums += int(a[0]) * 60
sums += float(a[1])
return sums
def apply_start(self):
with open(self.name,'r',encoding='utf8') as f:
a = f.readlines()
table_all = []
for i in a:
match1 = re.findall('\[.*\]', i)
# print(match1)
table = []
for x in range(len(match1[0]) // 10):
j = x * 10
qie = match1[0][(0 + j):(j + 10)]
table.append(qie)
# print(table)
match2 = re.findall('[\u4e00-\u9fa5]+', i)
str_match2 = ''.join(match2)
# print(match2)
for m in table:
dict2 = {}
dict2[m] = str_match2
table_all.append(dict2)
# print(table_all)
table_all1 = []
for n in table_all:
dict3 = {}
for x in n:
key2 = x[1:9]
value2 = n[x]
dict3[key2] = value2
table_all1.append(dict3)
# print(table_all1)
table_all2 = []
for q in table_all1:
dict4 = {}
for w in q:
dict4[Apply.change(w)] = q[w]
table_all2.append(dict4)
# print(table_all2)
def s1(x):
for i in x:
return i
s = sorted(table_all2, key=s1)
# print(s)
second = float(input('秒数:'))
dict5 = {}
dict5[second] = 'None'
table_all2.append(dict5)
v = sorted(table_all2, key=s1)
z = v.index(dict5)
# print(z)
for l in table_all2[z - 1]:
return table_all2[z - 1][l]
a = Apply('song')
print(a.apply_start())