""" 1. 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。 至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法 """
class Auto:
def __init__(self, wheel, color, weight, speed=0):
self.wheel = wheel
self.color = color
self.weight = weight
self.speed = speed
self._max_speed = 200 def up_speed(self, value):
panel_speed = self.speed + value
if panel_speed > self._max_speed:
self._max_speed = panel_speed
return '超速行驶当前车速%s' % self._max_speed
else:
return '加速行驶车速%s' % panel_speed
def slow_down(self, value):
if self.speed > value:
return '减速行驶',self.speed - value
else:
return 0 def stop(self):
if self.speed == 0:
return '停车' a1 = Auto(4, 'balck', 5)
print(a1.up_speed(50))
a1.speed = 90 print(a1.slow_down(50))
class CarAuto(Auto):
def __init__(self, air_condition=False, cd_rom=False, color='black', speed=0):
super(CarAuto, self).__init__(color, speed, weight='5tons')
self.air_condition = air_condition
self.cd_rom = cd_rom def up_speed(self, value):
super(CarAuto, self).up_speed(value)
a2 = CarAuto(True, True)
print(a2.up_speed(200))
2. 创建一个Person类,添加一个类字段用来统计Person类的对象的个数
class Person:
count = 0
def __init__(self, name, age, gender):
self.name = name
self.gender = gender
if 0 < age <= 105:
self.age = age
else:
raise ValueError
# if 0 <= gender <= 1:
# self.gender = gender
#
# else:
# raise ValueError
self.statistics()
@classmethod
def statistics(cls):
cls.count += 1
p1 = Person('MING', 22, '男')
print(Person.count)
p2 = Person('LI-NING', 56, '男')
print(Person.count)
3. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 。
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self, gender, age, color, species):
self._age = age
self.color = color
self.species = species
if not gender or gender == 1:
self._gender = gender
else:
raise ValueError
@property
def gender(self):
if not self._gender:
return 'Male'
if self._gender == 1:
return 'Female'
@gender.setter
def gender(self, value):
raise ValueError
def __repr__(self):
return '/{}的对象:性别-{}, 年龄-{}, 颜色-{}, 种类{}/'.format(self.__class__.__name__, self.gender, self._age, self.color, self.species)
a1 = Animal(1, 5, 'black', '曼康基')
print(a1)
4. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class Circle:
Pi = 3.1414926
def __init__(self, radius):
self.radius = radius
self._area = Circle.Pi * self.radius **2
self._perimeter = 2 * Circle.Pi * self.radius
@property
def area(self):
self._area = Circle.Pi * self.radius **2
return self._area
@area.setter
def area(self, value):
raise ValueError
@property
def perimeter(self):
self._perimeter = 2 * Circle.Pi * self.radius
return self._perimeter
@perimeter.setter
def perimeter(self, value):
raise ValueError
def __repr__(self):
return '/radius:{}, area:{}, perimeter:{}/'.format(self.radius, self.area, self.perimeter)
c1 = Circle(10)
print(c1)
5. 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
KL;‘/.
class Poker:
def __init__(self, color, ajqk):
self.color = color
self.ajqk = ajqk
class Pokers:
color_list = ['♦', '♥', '♠', '♣']
ajqk_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.ace_of_Base = []
for color in Pokers.color_list:
for ajqk in Pokers.ajqk_list:
self.poker_list.append(Poker(color, ajqk))
self.poker_list.append(Poker('Red', 'Joker'))
self.poker_list.append(Poker('Black', 'Joker'))
def poker_shuffle(self):
random.shuffle(self.poker_list)