1.
class guns():
def __init__(self, model, bullets):
self.model = model
self.bullets = bullets
def shoot(self):
if self.bullets > 0:
self.bullets -= 1
print(f"使用{self.model}发射成功,子弹还剩{self.bullets}发")
else:
print(f"没子弹了,请装弹")
def reload(self):
self.bullets = 30
print(f"装弹成功,子弹还剩{self.bullets}发")
class soldier():
def __init__(self, name, gun):
self.name = name
self.gun = gun
def shoot_s(self):
self.gun.shoot()
def reload_s(self):
self.gun.reload()
ak47 = guns('ak47', 30)
zs = soldier('zhangsan', ak47)
print(f"士兵的名字是{zs.name}")
for i in range(33):
if zs.gun.bullets > 0:
zs.shoot_s()
else:
zs.shoot_s()
zs.reload_s()
优化:
class guns():
def __init__(self, model, bullets):
self.model = model
self.bullets = bullets
self.a = ''
def __str__(self):
return f'{self.model}有{self.bullets}颗子弹'
def shoot(self):
if self.bullets > 0:
self.bullets -= 1
print(f"{self.a}使用{self.model}发射成功,子弹还剩{self.bullets}发")
else:
print(f"没子弹了,请装弹")
def reload(self):
self.bullets = 30
print(f"装弹成功,子弹还剩{self.bullets}发")
class soldier():
def __init__(self, name):
self.name = name
self.gun = 0
def __str__(self):
if self.gun == 0:
return f"士兵的名字是{self.name},没有配枪"
else:
return f"士兵的名字是{self.name},配枪:{self.gun}"
def fire(self):
if self.gun == 0:
print(f"{self.name}没有配枪,请配枪")
else:
self.gun.a = self.name
self.gun.shoot()
# 创建一把枪AK47,并装上30颗子弹
ak47 = guns('ak47', 30)
print(ak47)
# 创建一个士兵张三
zs = soldier('张三')
print(zs)
zs.fire()
# 给张三配枪AK47
zs.gun = ak47
print(zs)
for i in range(32): # 扫空弹夹并自动装弹
if zs.gun.bullets > 0:
zs.fire()
else:
zs.fire()
zs.gun.reload()
2.
class car1():
def __init__(self, colors, wheels, weight, speed):
self.colors = colors
self.wheels = wheels
self.weight = weight
self.speed = speed
def speed_up(self):
print("car1 speed_up")
def slow_down(self):
print("car1 slow_down")
def pull_over(self):
print("pull_over")
class car2(car1):
def __init__(self, colors, wheels, weight, speed, name, model, AirConditioningSystems):
super().__init__(colors, wheels, weight, speed)
self.name = name
self.model = model
self.AirConditioningSystems = AirConditioningSystems
def speed_up(self):
print("car2 speed_up")
def slow_down(self):
print("car2 slow_down")
def print_car2(self):
print(self.__dict__)
ae86 = car2('white', 4, 925, 160, 'AE86', 'TOYOTA', 'AirConditioning')
ae86.speed_up()
ae86.slow_down()
ae86.print_car2()
3.
class police_dog():
def __init__(self, dog_name):
self.dog_name = dog_name
def run(self):
print(f"{self.dog_name}正在追击敌人")
def smell(self):
print(f"{self.dog_name}正在搜查毒品")
class police():
def __init__(self, name, dog):
self.name = name
self.dog = dog
def pursue(self):
print(f"{self.name}带着警犬{self.dog.dog_name}执行追击任务")
self.dog.run()
def search(self):
print(f"{self.name}带着警犬{self.dog.dog_name}执行缉毒任务")
self.dog.smell()
wangcai = police_dog('旺财')
xiaohei = police_dog('小黑')
police1 = police('张三', wangcai)
police2 = police('李四', xiaohei)
police1.pursue()
police2.search()
优化:
class police_dog():
def work(self):
pass
class pursue(police_dog):
def work(self):
print("追击")
class search(police_dog):
def work(self):
print("搜查")
class police():
def with_dog(self, dog):
dog.work()
p = pursue()
s = search()
zs = police()
zs.with_dog(p)
4.
def time1(t):
if t < 3 and t >= 0:
raise Exception("没熟")
elif t < 5 and t >= 3:
raise Exception("半生不熟")
elif t > 8:
raise Exception("焦了")
class pancake():
def __init__(self, t):
self.t = t
def try1(self):
try:
time1(self.t)
except Exception as e:
print(e)
else:
print("熟了")
def scallion(self):
print("加葱")
def garlic(self):
print("加蒜")
def sausage(self):
print("加肠")
while True:
try:
t = int(input('请输入时间'))
except:
print("输入错误请重新输入")
else:
a = pancake(t)
a.try1()
a.scallion()
a.garlic()
a.sausage()
break
优化:
class pancake():
def __init__(self):
self.t = int(input('请设置烹饪时间'))
self.seasoning = input('请加入佐料')
def cook(self):
if self.t < 3 and self.t >= 0:
print(f"你做了个没熟的{self.seasoning}煎饼")
elif self.t < 5 and self.t >= 3:
print(f"你做了个半生不熟的{self.seasoning}煎饼")
elif self.t < 8 and self.t >= 5:
print(f"你做了个美味的{self.seasoning}煎饼")
elif self.t > 8:
print(f"你做了个焦了的{self.seasoning}煎饼")
else:
raise Exception
while True:
try:
pc = pancake()
pc.cook()
except:
print("请输入正确的时间")
else:
break