1.
    image.png

    1. class guns():
    2. def __init__(self, model, bullets):
    3. self.model = model
    4. self.bullets = bullets
    5. def shoot(self):
    6. if self.bullets > 0:
    7. self.bullets -= 1
    8. print(f"使用{self.model}发射成功,子弹还剩{self.bullets}发")
    9. else:
    10. print(f"没子弹了,请装弹")
    11. def reload(self):
    12. self.bullets = 30
    13. print(f"装弹成功,子弹还剩{self.bullets}发")
    14. class soldier():
    15. def __init__(self, name, gun):
    16. self.name = name
    17. self.gun = gun
    18. def shoot_s(self):
    19. self.gun.shoot()
    20. def reload_s(self):
    21. self.gun.reload()
    22. ak47 = guns('ak47', 30)
    23. zs = soldier('zhangsan', ak47)
    24. print(f"士兵的名字是{zs.name}")
    25. for i in range(33):
    26. if zs.gun.bullets > 0:
    27. zs.shoot_s()
    28. else:
    29. zs.shoot_s()
    30. zs.reload_s()

    优化:

    1. class guns():
    2. def __init__(self, model, bullets):
    3. self.model = model
    4. self.bullets = bullets
    5. self.a = ''
    6. def __str__(self):
    7. return f'{self.model}有{self.bullets}颗子弹'
    8. def shoot(self):
    9. if self.bullets > 0:
    10. self.bullets -= 1
    11. print(f"{self.a}使用{self.model}发射成功,子弹还剩{self.bullets}发")
    12. else:
    13. print(f"没子弹了,请装弹")
    14. def reload(self):
    15. self.bullets = 30
    16. print(f"装弹成功,子弹还剩{self.bullets}发")
    17. class soldier():
    18. def __init__(self, name):
    19. self.name = name
    20. self.gun = 0
    21. def __str__(self):
    22. if self.gun == 0:
    23. return f"士兵的名字是{self.name},没有配枪"
    24. else:
    25. return f"士兵的名字是{self.name},配枪:{self.gun}"
    26. def fire(self):
    27. if self.gun == 0:
    28. print(f"{self.name}没有配枪,请配枪")
    29. else:
    30. self.gun.a = self.name
    31. self.gun.shoot()
    32. # 创建一把枪AK47,并装上30颗子弹
    33. ak47 = guns('ak47', 30)
    34. print(ak47)
    35. # 创建一个士兵张三
    36. zs = soldier('张三')
    37. print(zs)
    38. zs.fire()
    39. # 给张三配枪AK47
    40. zs.gun = ak47
    41. print(zs)
    42. for i in range(32): # 扫空弹夹并自动装弹
    43. if zs.gun.bullets > 0:
    44. zs.fire()
    45. else:
    46. zs.fire()
    47. zs.gun.reload()

    2.
    image.png

    1. class car1():
    2. def __init__(self, colors, wheels, weight, speed):
    3. self.colors = colors
    4. self.wheels = wheels
    5. self.weight = weight
    6. self.speed = speed
    7. def speed_up(self):
    8. print("car1 speed_up")
    9. def slow_down(self):
    10. print("car1 slow_down")
    11. def pull_over(self):
    12. print("pull_over")
    13. class car2(car1):
    14. def __init__(self, colors, wheels, weight, speed, name, model, AirConditioningSystems):
    15. super().__init__(colors, wheels, weight, speed)
    16. self.name = name
    17. self.model = model
    18. self.AirConditioningSystems = AirConditioningSystems
    19. def speed_up(self):
    20. print("car2 speed_up")
    21. def slow_down(self):
    22. print("car2 slow_down")
    23. def print_car2(self):
    24. print(self.__dict__)
    25. ae86 = car2('white', 4, 925, 160, 'AE86', 'TOYOTA', 'AirConditioning')
    26. ae86.speed_up()
    27. ae86.slow_down()
    28. ae86.print_car2()

    3.
    image.png

    1. class police_dog():
    2. def __init__(self, dog_name):
    3. self.dog_name = dog_name
    4. def run(self):
    5. print(f"{self.dog_name}正在追击敌人")
    6. def smell(self):
    7. print(f"{self.dog_name}正在搜查毒品")
    8. class police():
    9. def __init__(self, name, dog):
    10. self.name = name
    11. self.dog = dog
    12. def pursue(self):
    13. print(f"{self.name}带着警犬{self.dog.dog_name}执行追击任务")
    14. self.dog.run()
    15. def search(self):
    16. print(f"{self.name}带着警犬{self.dog.dog_name}执行缉毒任务")
    17. self.dog.smell()
    18. wangcai = police_dog('旺财')
    19. xiaohei = police_dog('小黑')
    20. police1 = police('张三', wangcai)
    21. police2 = police('李四', xiaohei)
    22. police1.pursue()
    23. police2.search()

    优化:

    1. class police_dog():
    2. def work(self):
    3. pass
    4. class pursue(police_dog):
    5. def work(self):
    6. print("追击")
    7. class search(police_dog):
    8. def work(self):
    9. print("搜查")
    10. class police():
    11. def with_dog(self, dog):
    12. dog.work()
    13. p = pursue()
    14. s = search()
    15. zs = police()
    16. zs.with_dog(p)

    4.
    image.png

    1. def time1(t):
    2. if t < 3 and t >= 0:
    3. raise Exception("没熟")
    4. elif t < 5 and t >= 3:
    5. raise Exception("半生不熟")
    6. elif t > 8:
    7. raise Exception("焦了")
    8. class pancake():
    9. def __init__(self, t):
    10. self.t = t
    11. def try1(self):
    12. try:
    13. time1(self.t)
    14. except Exception as e:
    15. print(e)
    16. else:
    17. print("熟了")
    18. def scallion(self):
    19. print("加葱")
    20. def garlic(self):
    21. print("加蒜")
    22. def sausage(self):
    23. print("加肠")
    24. while True:
    25. try:
    26. t = int(input('请输入时间'))
    27. except:
    28. print("输入错误请重新输入")
    29. else:
    30. a = pancake(t)
    31. a.try1()
    32. a.scallion()
    33. a.garlic()
    34. a.sausage()
    35. break

    优化:

    1. class pancake():
    2. def __init__(self):
    3. self.t = int(input('请设置烹饪时间'))
    4. self.seasoning = input('请加入佐料')
    5. def cook(self):
    6. if self.t < 3 and self.t >= 0:
    7. print(f"你做了个没熟的{self.seasoning}煎饼")
    8. elif self.t < 5 and self.t >= 3:
    9. print(f"你做了个半生不熟的{self.seasoning}煎饼")
    10. elif self.t < 8 and self.t >= 5:
    11. print(f"你做了个美味的{self.seasoning}煎饼")
    12. elif self.t > 8:
    13. print(f"你做了个焦了的{self.seasoning}煎饼")
    14. else:
    15. raise Exception
    16. while True:
    17. try:
    18. pc = pancake()
    19. pc.cook()
    20. except:
    21. print("请输入正确的时间")
    22. else:
    23. break