2016-12-18 109 views
1

我想创建切换案例,但不知道如何从另一个函数调用一个函数。例如,在此代码中,当我按“O”时,我想调用OrderModule()如何在一个类中调用另一个函数(方法)?

其实我已经在Java中完成了整个程序,也许有人知道,如何在不重写所有程序的情况下更轻松地转换它?

class CMS: 

    def MainMenu(): 
     print("|----Welcome to Catering Management System----|") 

     print("|[O]Order") 
     print("|[R]Report") 
     print("|[P]Payment") 
     print("|[E]Exit") 
     print("|") 
     print("|----Select module---- \n") 
     moduleSelect = raw_input() 
     if moduleSelect == "o" or moduleSelect == "O": 
      --- 
     if moduleSelect == "r" or moduleSelect == "R": 
      --- 
     if moduleSelect == "P" or moduleSelect == "P": 
      --- 
     if moduleSelect == "e" or moduleSelect == "E": 
      --- 
     else: 
      print("Error") 
    MainMenu() 
    def OrderModule(): 
     print("|[F]Place orders for food") 
     print("|[S]Place orders for other services") 
     print("|[M]Return to Main Menu") 
    OrderModule() 
+0

你问如何调用一个函数? – Carcigenicate

+0

是的,像java中的方法 –

+0

'self'是Python中的关键字,它应该作为第一个方法的参数显式传递。此外,可以使用策略设计模式重写此代码。 – Nevertheless

回答

1

这是交易。为了您对Python的理解,我会对代码进行一些重构,也许会提供一些关于设计模式的小技巧。

请认为这个例子过于简单并且有些过分,它的目的是为了提升你新兴的开发技能。

首先,您最好熟悉Strategy Design Pattern,这对于这些任务(我个人认为)来说非常方便。之后,您可以创建一个基本模块类和它的策略。 通知如何self(可变表示对象本身的实例),其为第一个参数显式地传递到类的方法

class SystemModule(): 
    strategy = None 

    def __init__(self, strategy=None): 
     ''' 
     Strategies of this base class should not be created 
     as stand-alone instances (don't do it in real-world!). 
     Instantiate base class with strategy of your choosing 
     ''' 
     if type(self) is not SystemModule: 
      raise Exception("Strategy cannot be instantiated by itself!") 
     if strategy: 
      self.strategy = strategy() 

    def show_menu(self): 
     ''' 
     Except, if method is called without applied strategy 
     ''' 
     if self.strategy: 
      self.strategy.show_menu() 
     else: 
      raise NotImplementedError('No strategy applied!') 


class OrderModule(SystemModule): 
    def show_menu(self): 
     ''' 
     Strings joined by new line are more elegant 
     than multiple `print` statements 
     ''' 
     print('\n'.join([ 
      "|[F]Place orders for food", 
      "|[S]Place orders for other services", 
      "|[M]Return to Main Menu", 
     ])) 


class ReportModule(SystemModule): 
    def show_menu(self): 
     print('---') 


class PaymentModule(SystemModule): 
    def show_menu(self): 
     print('---') 

这里OrderModuleReportModulePaymentModule可以被定义为第一级的功能,但这个例子中类更明显。接下来,创建一个主类应用程序的:

class CMS(): 
    ''' 
    Options presented as dictionary items to avoid ugly 
    multiple `if-elif` construction 
    ''' 
    MAIN_MENU_OPTIONS = { 
     'o': OrderModule, 'r': ReportModule, 'p': PaymentModule, 
    } 

    def main_menu(self): 
     print('\n'.join([ 
      "|----Welcome to Catering Management System----|", "|", 
      "|[O]Order", "|[R]Report", "|[P]Payment", "|[E]Exit", 
      "|", "|----Select module----", 
     ])) 

     # `raw_input` renamed to `input` in Python 3, 
     # so use `raw_input()` for second version. Also, 
     # `lower()` is used to eliminate case-sensitive 
     # checks you had. 
     module_select = input().lower() 

     # If user selected exit, be sure to close app 
     # straight away, without further unnecessary processing 
     if module_select == 'e': 
      print('Goodbye!') 
      import sys 
      sys.exit(0) 

     # Perform dictionary lookup for entered key, and set that 
     # key's value as desired strategy for `SystemModule` class 
     if module_select in self.MAIN_MENU_OPTIONS: 
      strategy = SystemModule(
       strategy=self.MAIN_MENU_OPTIONS[module_select]) 

      # Base class calls appropriate method of strategy class 
      return strategy.show_menu() 
     else: 
      print('Please, select a correct module') 

对于这整个事情的工作,有一个在文件末尾简单首发:

if __name__ == "__main__": 
    cms = CMS() 
    cms.main_menu() 

在这里你去。我真的希望这段代码能够帮助你深入Python:) 干杯!

相关问题