1、建立一个汽车类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
return self.speed
def speed_cut(self, value):
if self.speed <= value:
raise ValueError
else:
self.speed -= value
return self.speed
def park(self, value):
if self.speed <= value:
return '停车成功!'
else:
return '未成功停车!'
class CarAuto(Auto):
def __init__(self, tyre, color, weight, speed, air, CD):
super(CarAuto, self).__init__(tyre, color, weight, speed)
self.air = air
self.CD = CD
def speed_up(self, value):
self.speed += value
return self.speed
def speed_cut(self, value):
if self.speed <= value:
raise ValueError
else:
self.speed -= value
return self.speed
def main():
c = CarAuto(4, 'white', '1500kg', 60, '格力', 'cd')
print(c.speed_cut(10))
print(c.speed_cut(10))
print(c.speed_cut(10))
print(c.park(30))
if __name__ == '__main__':
main()
2、创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self):
Person.count += 1
p1 = Person()
p2 = Person()
print(Person.count)
3、创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self, gender, age, color, form):
self.gender = gender
self.age = age
self.color = color
self.form = form
def print_res(self):
print(f'/{self.__class__.__name__}的对象:性别-{self.gender} 年龄-{self.age} 颜色-{self.color} 类型-{self.form}/')
a = Animal('male', 3, 'white', '哺乳')
a.print_res()
4、写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。
但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class Circle:
pi = 3.14
def __init__(self, radius):
self.radius = radius
self._area = 0
self._perimeter = 0
@property
def area(self):
self._area = Circle.pi * self.radius ** 2
return self._area
@area.setter
def area(self, value):
print('改属性不能赋值')
raise ValueError
@property
def perimeter(self):
self._perimeter = 2 * Circle.pi * self.radius
return self._perimeter
@perimeter.setter
def perimeter(self, value):
print('改属性不能被赋值')
raise ValueError
c = Circle(3)
print(c.area)
c.area = 3
print(c.perimeter)
c.perimeter = 2
5、写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random
class Poker:
def __init__(self):
self.player_A = []
self.player_B = []
self.player_C = []
self.str_poker = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', ]
self.color_poker = ['♠', '♣', '♦', '♥']
self.joker = ['大王', '小王']
def shuffle_poker(self):
"""洗牌"""
for i in self.str_poker:
for j in self.color_poker:
self.joker.append(j+i)
random.shuffle(self.joker)
return self.joker
def deal(self):
"""发牌"""
self.shuffle_poker()
for i in range(0, 51, 3):
self.player_A.append(self.joker[i])
print('玩家A:', self.player_A)
for j in range(1, 51, 3):
self.player_B.append(self.joker[j])
print('玩家B:', self.player_B)
for k in range(2, 51, 3):
self.player_C.append(self.joker[k])
print('玩家C:', self.player_C)
for l in self.player_A + self.player_B + self.player_C:
self.joker.remove(l)
print('地主:', self.joker)
p = Poker()
p.deal()