我正在用Python创建一系列文本菜单。Python惯例告诉我,我应该只为需要维护和操作的数据成员生成类/对象。如果是这样,我不确定……下面是合适的还是创建一个基本功能,然后根据哪个菜单来装饰它更好?在
class Menu(object):
def __init__(self):
# Ideally, self._options would be an empty dict for the base class.
# This is just for the sake of example.
self._options = {'a': self.optionA,
'b': self.optionB}
def handle_options(self, option):
if option not in self._options:
print "Invalid option"
# re-draw
return
self._options[option]()
def optionA(self):
print "option A"
def optionB(self):
print "option B"
class SubMenu(Menu):
def __init__(self):
Menu.__init__(self)
self._options = {'c': self.optionC,
'd': self.optionD}
def optionC(self):
# ...
def optionD(self):
# .
。。在
这样做的方法是有一个
Menu
基类,它只提供行为,比如显示自己、下拉菜单、将附加菜单弹出到右侧、隐藏自身等然后让子类包含每个菜单实例的实际数据。在
相关问题 更多 >
编程相关推荐