0}. 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self, weight, speed, tyre_num= 4, color = 'white'):
self.weight = weight
self.tyre_num = tyre_num
self.color = color
self.speed = speed
def speed_up(self):
self.speed+=5
print('当前速度:%skm/h'%(self.speed))
def speed_down(self):
self.speed-=5
print('当前速度:%skm/h'%(self.speed))
class CarAuto(Auto):
def __init__(self,*args,**kwargs) :
super().__init__(*args,**kwargs)
self.air_conditioner = 'air_conditioner'
self.CD = 'CD'
def speed_up(self):
self.speed += 10
print('当前速度:%skm/h' % (self.speed))
def speed_down(self):
self.speed -= 10
print('当前速度:%skm/h' % (self.speed))
car1 = CarAuto('1200KG',80)
print(car1.color,car1.tyre_num,car1.speed)
car1.speed_down()
car1.speed_down()
car1.speed_up()
{0}. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self):
Person.count += 1
p1 = Person()
P2 = Person()
p3 = Person()
print(Person.count)
{0}. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self,gender,age,color,kinds):
self.gender = gender
self.age = age
self.color = color
self.kinds = kinds
def __repr__(self):
return '{}对象:性别:{}年龄:{}颜色:{}品种:{}'.format(self.__class__.__name__,self.gender,self.age,self.color,self.kinds)
animal1 = Animal('雄',18,'white','羊')
print(animal1)
{0}. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class Circle:
pi = 3.1415926
def __init__(self,r,c = None,area = None):
self.r = r
self._c = c
self._area = area
@property
def c(self):
if self._c == None:
self._c = self.r * Circle.pi * 2
return self._c
else:
print('周长不能直接赋值')
raise str(ValueError)
@property
def area(self):
if self._area == None:
self._area = ((self.r)**2) * Circle.pi
return self._area
else:
print('面积不能直接赋值')
raise ValueError
c1 = Circle(5)
print(c1.r,c1.c,c1.area)
c2 = Circle(2,6.28,100)
print(c2.area,c2.c)
{0}. 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
class Onepear:
hongt = []
heit = []
meihua = []
fangp = []
boss = []
for i in range(1,11):
pai_hongt = '红桃'+str(i)
pai_heit = '黑桃'+str(i)
pai_meihua = '梅花'+str(i)
pai_fangp = '方片' +str(i)
hongt.append(pai_hongt)
heit.append(pai_heit)
meihua.append(pai_meihua)
fangp.append(pai_fangp)
list4 = ['J','Q','K']
for i in list4:
hongt.append('红桃'+i)
heit.append('黑桃'+i)
meihua.append('梅花'+i)
fangp.append('方片'+i)
boss = ['大王', '小王']
one_pear = boss+hongt+heit+meihua+fangp
import random
class Puker:
one_pear = Onepear.one_pear
@classmethod
def xipai(cls):
random.shuffle(cls.one_pear)
@classmethod
def fapai(cls):
iter1 = iter(cls.one_pear)
return next(iter1)
puker1 = Puker
puker1.xipai()
print(puker1.one_pear)
print(puker1.fapai())