笨办法学python3 学习笔记 习题43 基本的面向对象分析和设计

习题43 基本的面向对象分析和设计

通过面向对象编程(OOP)构建一些东西的流程(常用)(自顶向下)

  1. 把要解决的问题写下来,或者画出来。
  2. 将第一条中的关键概念提取出来并加以研究。
  3. 创建一个类层次结构和对象图。
  4. 用代码实现各个类,并写一个测试来运行它们。
  5. 重复上述步骤并细化代码。

(自底向上)

  1. 取出要解决的问题中的一小块,写些代码让它差不多能工作。
  2. 加上类和自动测试,细化代码让它更为正式。
  3. 把关键概念抽取出来然后研究它们。
  4. 把真正需要实现的东西描述出来。
  5. 回去细化代码,有可能需要全部丢弃重头做起。
  6. 在问题的另外一小块里重复上述流程。
from sys import exit
from random import randint
from textwrap import dedent

# 场景
class Scene(object):

    def enter(self):
        print("该场景尚未配置。")
        print("将其子类化并将enter()补充完整。")
        exit(1)

# 引擎
class Engine(object):

    def __init__(self, scene_map):
        self.scene_map = scene_map

    ## 在play运行的开始,current-scene和last-scene是不等的,后来使current_scene变化,令其等于始终未变的last_scene,以确保游戏进行到下一个场景
    def play(self):
        print(">>>>>>>>>>>start play")
        current_scene = self.scene_map.opening_scene()          # 返回开始场景的键值对对应的类
        last_scene = self.scene_map.next_scene('finished')      # 返回Finished()类
        print(">>>>>>>>>>>>>>>>after play, current_scene:", current_scene, "last_scene:", last_scene)

        print("^^^^^^^^^^^^^^^ before while, current_scene:", current_scene, "last_scene:", last_scene)
        while current_scene != last_scene:                      # 若当前场景非Finished()类
            print("&&&&&&&&&&&&&&&&&&&&&&before n = c, c =", current_scene, "last_scene", last_scene)
            next_scene_name = current_scene.enter()             # 返回当前场景的enter()函数
            print("##################after n = c, current_scene:", current_scene, "last_scene:", last_scene, "next_scene_name:", next_scene_name)
            current_scene = self.scene_map.next_scene(next_scene_name)
        print(">>>>>>>>>>>>>>>>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值