习题 42、43从属关系及基本的面相对象的分析和设计
对象是一个类,是他的子类,例如:
鱼和泥鳅的关系: 泥鳅是鱼的一种,是鱼的一个品种
小李和泥鳅的关系: 设小李是一条泥鳅则小李就是一个泥鳅的实例,具有具体的属性
class A(object):
括号中的object在python3 中是可以省略的
在习题43 中的分析和设计中学到的是分析和设计问题的方法,在这个习题中可以先将具体的类写出来,在进行进一步的细分,最后用笔画出来理想的地图,然后是编写程序过程,可以先写好大体框架然后在慢慢向里面添加具体的东西
具体代码如下:
from sys import exit
from random import randint
from textwrap import dedent
class Scene(object):
def enter(self):
print("this scene is not yet configured.")
print("subclass it and implement enter().")
exit(1)
class Engine(object):
def __init__(self, scene_map):
self.scene_map = scene_map
def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene('finished')
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
class Death(Scene):
quips = [
"you died, you kinda suck at this.",
"your mom would be proud...if she were smarter.",
"such a luser.",
"i have a small puppy that's better at this.",
"you're worse than your dad's jokes."
]
def enter(self):
print(Death.quips[randint(0, len(self.quips)-1)])
exit(1)
class CentralCorridor(Scene):
def enter(self):
print(dedent("""
小明捡到一分钱
把它交到警察叔叔手里面
然后被夸奖了。
小活动氨基酸了空间啊就就加
截断加上了空间;啦
就asj
"""))
action = input(">>>")
if action == "shoot!":
print(dedent("""
jdklajlk jlkjdklj lkja lkj klja lk
jkd jalkj lkj lj aj
jd aj lkdj lkajlak
jdlk jalk j lkjlj and he eat you .
"""))
return 'death'
elif action == "dodge!":
print(dedent("""
jdklsajdlkjaskl jdklsjalkdsa jkdlajklsj jdklasjlk
jdkljsaifah j kl;d jajwkl kw jaj lk
dkj kjsd lkjwi khskfkj
"""))
return 'death'
elif action == "tell a joke":
print(dedent("""
lucky for you they made you learn gothon insults in
the academy. you tell the one gothon joke you know:
lbhe zbgure lfksjalj lk jlkajsdkljsad;jlakfjklajfla.
weapon armory door
"""))
return 'laser_weapon_armory'
else:
print("does not compute!")
return 'central_corridor'
class LaserWeaponArmory(Scene):
def enter(self):
print(dedent("""
you do a dive roll into weapon armory, crouch and scan
the room for more gothons that might be hiding. it's dead
quiet, too quiet. you stand up and run to the far side of
the room and find the neutron bomb in its container.
there's a keypad lock on the box and u need the code to get
the bomb out. if u get the code wwrong 10 times then the lock
closes forever and you can't get the bomb. the code is 3 digits.
"""))
code = "123"
guess = input("[keypad]> ")
guesses = 0
while guess != code and guesses < 10:
print("BZZZZEDDD")
guesses += 1
guess = input("[keypad]> ")
if guess == code:
print(dedent("""
djskaljlk jlkjdkljdhjskahjlk j jadj jalk
inthe right spot
"""))
return 'the_bridge'
else:
print(dedent("""
the lock buzzes one last time and then you hear a
sickening melting sound as the mechanism is fused
together, youdecide to sit there, and finally the
gothons blow uo the ship from their ship and you die.
"""))
return 'death'
class TheBridge(Scene):
def enter(self):
print(dedent("""
you burst on to brigr with the netron jdkslajkl jlajdlksajdlksa
jdklsjakjdlksajlkdj sjdklajldkj jdkslajs lkj kjdlj lajk lja j jdklajlk
dhjsjah hlkjdsajl jdjkl jdkjskjiwrwqrshcni ajiruksj and don't awnt to set it off
"""))
action = input("> ")
if action == "throw the bomb":
print(dedent("""
in a panic you throw the bomb at
you die konwing they will probably
blow uo when it goes off
"""))
return 'death'
elif action == "slowly place the bomb":
print(dedent("""
dsadas dawrq fagrg gthtgx gsdegeg qwrq
qwrqgsvew rwqfaget gegsdvq gaga3wq tqwt
escape pod to get off this tin can.
"""))
return "escape_pod"
else:
print("DOES NOT COMPUTE!")
return "the_bridge"
class EscapePod(Scene):
def enter(self):
print(dedent("""
u rush through the ship desperately trying to make it to
the escape pod ........there's 5 pods, which one do you takes
"""))
good_pod = randint(1,5)
guess = input("[pod #]> ")
if int(guess) != good_pod:
print(dedent(f"""
you jump into pod {guess} and hit the eject button.
the pod escapes out .........crushing your body into
jam jelly
"""))
return 'death'
else:
print(dedent(f"""
you jump into pod {guess} and hit the eject button.
the pod .........taking out the gothon ship at the
same time. you won!
"""))
return 'finished'
class Finished(Scene):
def enter(self):
print("you win, good job!")
return 'finished'
class Map(object):
scenes = {
'central_corridor' : CentralCorridor(),
'laser_weapon_armory' : LaserWeaponArmory(),
'the_bridge' : TheBridge(),
'escape_pod' : EscapePod(),
'death' : Death(),
'finished' : Finished(),
}
def __init__(self, start_scene):
self.start_scene = start_scene
def next_scene(self, scene_name):
val = Map.scenes.get(scene_name)
return val
def opening_scene(self):
return self.next_scene(self.start_scene)
a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()
为了方便起见在这个程序中我将打印部分 做了简单的输入。
习题44 继承和组合
这个习题的重点是掌握子类的继承方式,和组合方式
重点注意
super().__init__()
的用法。至于要替换父类中的某个属性可以在子类中重新定义
组合是两个不同的类之间的组合并非是父子关系