- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self, tyre, color, weight, speed):
self.tyre = tyre
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self, value):
self.speed += value
print("您的当前车速为 {}".format(self.speed))
if self.speed >= 100:
print('您的车速过快,请减速慢行!')
raise ValueError
def speed_down(self, value):
self.speed -= value
if self.speed < 0:
print("您当前的车速为0已停车,下车请注意安全!")
elif self.speed < 100:
print("您的当前车速为 {}".format(self.speed))
print("您还可以继续加速!")
a1 = Auto(12, 'white', '10吨', speed=100)
print(a1.tyre, a1.color, a1.weight, a1.speed)
a1.speed_down(70)
class CarAuto(Auto):
def __init__(self, tyre, color, weight, air_condition, cd, speed):
super().__init__(tyre, color, weight, speed)
self.air_condition = air_condition
self.cd = cd,
def speed_up(self, value):
self.speed += value
if self.speed >= 120:
print("您的当前车速为 {}".format(self.speed))
print('您的车速过快,请减速慢行!')
def speed_down(self, value):
self.speed -= value
if self.speed < 0:
print("您当前的车速为0已停车,下车请注意安全!")
elif self.speed < 120:
print("您的当前车速为 {}".format(self.speed))
print("您还可以继续加速!")
c1 = CarAuto(4, 'white', '1.5吨', air_condition='media', cd='静悄悄', speed=80)
print(c1.tyre, c1.color, c1.weight, c1.air_condition, c1.cd, c1.speed)
c1.speed_up(80)
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self):
self.name = 'ming'
Person.count += 1
p1 = Person()
p2 = Person()
p3 = p2
print(Person.count)
- 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self, name, sex, age, color, species):
self.name = name
self.sex = sex
self.age = age
self.color = color
self.species = species
def __repr__(self):
print("{}的对象: 名称-{} 性别-{} 年龄-{} 颜色-{} 类型-{}".format(self.__class__,
self.name, self.sex, self.age, self.color, self.species))
a1 = Animal('tiger', 'male', 8, 'brown', 'feline')
a1.__repr__()
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
from math import pi
class WriteError(Exception):
def __str__(self):
return '该属性不能赋值'
class Circle:
def __init__(self, radius):
if not (isinstance(radius, int) or (isinstance(radius, float))):
raise ValueError
self.radius = radius
self._area = 0
self._perimeter = 0
@property
def area(self):
return pi*self.radius**2
@area.setter
def area(self, value):
raise WriteError
@property
def perimeter(self):
return 2*pi*self.radius
@perimeter.setter
def perimeter(self, value):
raise WriteError
c1 = Circle(4)
print(c1.area, c1.perimeter)
c1.radius = 8
print(c1.area, c1.perimeter)
c1.area = 23
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random
class Poker:
# 创建一副牌
colors = ['♥', '♠', '♣', '♦']
numbers = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
kings = [('black', '小王'), ('red', '大王')]
pokers = []
for color in colors:
for num in numbers:
pokers.append((color, num))
pokers.extend(kings)
def __init__(self, landowner):
self.landowner = landowner # 定义地主牌
# 洗牌
def shuffle(self):
random.shuffle(Poker.pokers)
return Poker.pokers
# 发牌
def deal(self):
deal_pokers = iter(Poker.shuffle(self))
# 创建玩家
player1 = []
player2 = []
player3 = []
# 拿出地主牌
landowner_poker = [next(deal_pokers), next(deal_pokers), next(deal_pokers)]
# 给玩家发牌
num = 1
for _ in range(51):
if num == 1:
player1.append(next(deal_pokers))
elif num == 2:
player2.append(next(deal_pokers))
elif num == 3:
player3.append(next(deal_pokers))
num = 0
num += 1
# 确定地主
if self.landowner in player1:
player1.extend(landowner_poker)
elif self.landowner in player2:
player2.extend(landowner_poker)
elif self.landowner in player3:
player3.extend(landowner_poker)
print(player1)
print(player2)
print(player3)
p1 = Poker(('black', '小王'))
p1.deal()
- (尝试)写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词