2016-12-18 108 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:) 乾杯!

相關問題