-
from math import pi from random import shuffle """ 1. 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法。Tires, color, weight, speed, Accelerate, decelerate, stop """ class Auto(): def __init__(self, tires=1, color='黑', weight='三吨', speed=30): self.tires = tires self.color = color self.weight = weight self.speed = speed def accelerate(self, add_speed): self.speed = add_speed + self.speed return f'当前加速后速度为:"{self.speed}"' def decelerate(self, reduce_speed): self.speed = self.speed - reduce_speed return f'当前加速后速度为:"{self.speed}"' @staticmethod def stop(): return "停车" class CarAuto(Auto): def __init__(self, air, cd): super().__init__() self.air = air self.cd = cd def accelerate(self, a=0, t=0): self.speed = self.speed + a * t return f'当前加速后速度为:"{self.speed}"' def decelerate(self, a=0, t=0): self.speed = self.speed - a * t return f'当前减速后速度为:"{self.speed}"' a1 = Auto(3, '黑色', '3吨', 30) print(a1.accelerate(20)) print(a1.decelerate(20)) print(a1.stop()) b1 = CarAuto("美的", '迈克尔') print(b1.__dict__) print("加速后速度", b1.accelerate(5, 10)) print("减速后速度", b1.accelerate(3, 10)) """ 2. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数 """ class Person(): n = 0 def __init__(self, name, age, gender, height, weight=60): self.name = name self.age = age self.gender = gender self.height = height self.weight = weight Person.n += 1 """ def __repr__(self, *args, **kwargs): Person.n = Person.n + 1 return f'{Person} {hex(id(Person))}' def test(self): Person.n = Person.n + 2 return Person.n @classmethod def quantity(cls): cls.n += 1 return cls.n """ c1 = Person(1, 1, 1, 1, 120) c2 = Person(2, 2, 2, 2, 180) print(c1.n) # print(c1) # print(c1.quantity()) print(id(c1)) print(id(Person(1, 1, 1, 1, 120))) print(0x00000273AE44F808) # <__main__.Person object at 0x00000235066DF808> # <class '__main__.Person'> 0x00000160fb9d0948 """ 3. 创建一个动物类,拥有属性:性别、年龄、颜色、类型。 要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印 """ class Animal(): def __init__(self, animal1, gender, age, color, kind1): self.animal1 = animal1 self.gender = gender self.age = age self.color = color self.kind1 = kind1 def __repr__(self): return f'/{self.animal1}的对象:{self.gender}, {self.age}, {self.color}, {self.kind1 }/' d1 = Animal('泼皮', '雄', '6', '灰白', '阿拉斯加') print(d1) """ 4. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值 """ class Circle(): def __init__(self, r): self.r = r self._acreages = r ** 2 * pi self._perimeter = 2 * pi * r @property def acreages(self): return self._acreages @property def perimeter(self): return self._perimeter @acreages.setter def acreages(self, value): print('这个变量不允许赋值') raise ValueError @perimeter.setter def perimeter(self, value): print('这个变量不允许赋值') raise ValueError """ b1 = Circle(3) b1.acreages = 60 b1.perimeter = 120 print(b1.acreages) print(b1.perimeter) print(b1.__dict__) """ """ 5. 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥) # iter1 = iter(str_dz1) # print(str_dz1) """ class Poker(): str_d = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'Q', 'K', 'A'] str_z = ['大王', '小王'] str_h = ['♥', '♠', '♣', '♦'] str_dz = [] z = 53 @classmethod def riffle(cls): for h in cls.str_h: for d in cls.str_d: hd = h + d cls.str_dz.append(hd) # 制造扑克牌 str_dz = cls.str_dz + cls.str_z shuffle(str_dz) return str_dz @classmethod def deal(cls): iter1 = iter(cls.str_dz) list1 =[] for x in range(3): b = next(iter1) list1.append(b) list2 = [] for y in range(16): a = next(iter1) list2.append(a) print(list2) print(list1) return "发牌完成" f1 = Poker() print(f1.riffle()) print(f1.deal())
day19-类与继承作业
最新推荐文章于 2022-11-26 20:27:03 发布