games2.py
来自「this is the most basic to learn python」· Python 代码 · 共 46 行
PY
46 行
#: c05:Games2.py
# Simplified Abstract Factory.
class Kitty:
def interactWith(self, obstacle):
print "Kitty has encountered a",
obstacle.action()
class KungFuGuy:
def interactWith(self, obstacle):
print "KungFuGuy now battles a",
obstacle.action()
class Puzzle:
def action(self): print "Puzzle"
class NastyWeapon:
def action(self): print "NastyWeapon"
# Concrete factories:
class KittiesAndPuzzles:
def makePlayer(self): return Kitty()
def makeObstacle(self): return Puzzle()
class KillAndDismember:
def makePlayer(self): return KungFuGuy()
def makeObstacle(self): return NastyWeapon()
class GameEnvironment:
def __init__(self, factory):
self.factory = factory
self.p = factory.makePlayer()
self.ob = factory.makeObstacle()
def play(self):
self.p.interactWith(self.ob)
g1 = GameEnvironment(KittiesAndPuzzles())
g2 = GameEnvironment(KillAndDismember())
g1.play()
g2.play()
#<hr>
output = '''
Kitty has encountered a Puzzle
KungFuGuy now battles a NastyWeapon
'''
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?