- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self, tyre_num, color, weight, speed):
self.tyre_num = tyre_num
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self, num):
while self.speed > 300:
print('不能加速了')
return
else:
self.speed = self.speed + num
def speed_down(self, num1):
if self.speed > 0:
self.speed -= num1
else:
print('该车未启动')
def stop(self):
if self.speed == 0:
print('该车已停车')
auto1 = Auto(4, 'black', 1000, 0)
auto1.speed_up(10)
auto1.speed_down(10)
print(auto1.speed)
class CarAuto(Auto):
def __init__(self, air_condition, cd, tyre_num, color, weight, speed):
super().__init__(tyre_num, color, weight, speed)
self.air_condition = air_condition
self.cd = cd
def speed_up(self, num):
while self.speed > 200:
print('不能加速了')
return
else:
self.speed = self.speed + num
def speed_down(self, num1):
if self.speed > 0:
self.speed -= num1
else:
print('该车未启动')
def stop(self):
if self.speed == 0:
print('该车已停车')
carauto2 = Auto(4, 'black', 700, 199)
carauto2.speed_up(10)
carauto2.speed_down(1)
print(carauto2.speed)
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Person.count += 1
person1 = Person('rr', 2)
person2 = Person('ll', 2)
print(Person.count)
-
创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self, gender, age, color, mold):
self.gender = gender
self.age = age
self.color = color
self.mold = mold
def name(self, name1):
print('%s的对象:性别-%s,年龄-%s,颜色-%s,类型-%s' % (name1, self.gender, self.age, self.color, self.mold))
animal1 = Animal('boy', 2, 'yellow', '中华田园犬')
animal1.name('Jack')
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class Circle:
def __init__(self, radius):
self._radius = radius
self._area = 0
self._perimeter = 0
@property
def area(self):
self._area = pi*self._radius**2
return self._area
@area.setter
def area(self, value):
raise Exception('Attribute cannot be assigned')
@property
def perimeter(self):
self._perimeter = 2*pi * self._radius
return self._perimeter
@perimeter.setter
def perimeter(self, value):
raise Exception('Attribute cannot be assigned')
circle1 = Circle(3)
print(circle1.area, circle1.perimeter)
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
class Poker:
poker_num = 54
def __init__(self):
self.club = ['♣2', '♣3', '♣4', '♣5', '♣6', '♣7', '♣8', '♣9', '♣10', '♣J', '♣Q', '♣K', '♣A']
self.diamonds = ['♦2', '♦3', '♦4', '♦5', '♦6', '♦7', '♦8', '♦9', '♦10', '♦J', '♦Q', '♦K', '♦A']
self.hearts = ['♥2', '♥3', '♥4', '♥5', '♥6', '♥7', '♥8', '♥9', '♥10', '♥J', '♥Q', '♥K', '♥A']
self.spade = ['♠2', '♠3', '♠4', '♠5', '♠6', '♠7', '♠8', '♠9', '♠10', '♠J', '♠Q', '♠K', '♠A']
self.joker = ['大王', '小王']
def riffle(self):
pokers = self.hearts + self.club + self.joker + self.spade + self.diamonds
random.shuffle(pokers)
print(pokers)
def deal(self):
person1 = int(input('请输入人数(3及以上):'))
if person1 == 3:
print('打斗地主')
pokers1 = self.hearts + self.club + self.joker + self.spade + self.diamonds
random.shuffle(pokers1)
poker_iter = iter(pokers1)
A = []
B = []
C = []
num = 1
for _ in range(17 * 3):
if num == 1:
A.append(next(poker_iter))
elif num == 2:
B.append(next(poker_iter))
elif num == 3:
C.append(next(poker_iter))
num = 0
num += 1
print(A)
print(B)
print(C)
else:
print('炸金花')
pass
poker1 = Poker()
poker1.riffle()
poker1.deal()
-
(尝试)写一个类,其功能是: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]蓝莲花