【问题标题】:Python Class Inheritance Function and Pass Parameters From ChildPython 类继承函数并从子级传递参数
【发布时间】:2013-06-24 23:51:45
【问题描述】:
class BaseMenu(object):
    def display(self):
        header = "FooBar YO"
        term = getTerminalSize()
        #sys.stdout.write("\x1b[2J\x1b[H")
        print header.center(term, '*')
        #print sub_menu.center(term, '+')
        print "Please choose which option:"
        for i in options:
            print(
                str(options.index(i)+1) + ") "
            )

class Servers(BaseMenu):
    def __init__(self):
        #super(Servers, self).__init__("server")
        pass

    def list_foo(self):
        pass
    def list_bar(self):
        pass
    options = (
        list_foo,
        list_bar
        )

尝试制作一系列以 Main Menu -> Servers 子菜单开头的文本菜单。当 Servers() 从 BaseClass 继承 display() 时,如何使继承的函数 display() 接收 Servers() 类中包含的选项 tuple 和 sub_menu = "Server Menu" 字符串?

【问题讨论】:

  • ncurses 对这类事情有很大帮助,不是吗?

标签: python class inheritance


【解决方案1】:

您可以在display 函数中使用self.options 和self.sub_menu,但是为什么在对options 或sub_menu 一无所知的BaseMenu 类中引用它们?

第二个问题,您将 "server" 参数传递给其 __init__ 不带参数的类,因此您需要添加它。

如果您打算从不实例化BaseMenu 对象,那么它是一个抽象基类(或ABC)。您可以使用 pythons abc 模块来定义它,以确保继承类定义您期望的属性:

import abc

class BaseMenu(object):
    __metaclass__ = abc.ABCMeta  #indicate that this is an ABC

    @abc.abstractproperty  # prevent derived classes that don't have options
    def options(self):
        pass

    @abc.abstractproperty
    def sub_menu(self):
        pass

    def __init__(self, menu_name): # takes the menu name as an argument ("server")
        self.menu_name = menu_name

    def display(self):
        header = "FooBar YO"
        term = getTerminalSize()
        print header.center(term, '*')
        print self.sub_menu.center(term, '+') # NOTE self.sub_menu
        print "Please choose which option:"
        for i in self.options: # NOTE self.options
            print(self.options.index(i)+1 + ") ")

如果任何类尝试从 BaseMenu 继承而不定义 options 和 sub_menu,它将在实例化时导致 TypeError 如下所示:

TypeError: Can't instantiate abstract class Servers with abstract methods options

【讨论】:

    【解决方案2】:

    我不确定我是否完全明白你在这里问什么,所以告诉我,这个怎么样?

    class BaseMenu(object):
    
        # Added some attributes here:
        menuName = ""
        options = ()
    
        def __init__(self, menu_name, opt):
            self.menuName = menu_name  # = "Servers Menu" when instantiated as Server
            self.options = opt         # the passed when instantiated as Server
    
        def display(self):
    
            # Use self.menuName and self.options here
    
            #...
            for i in self.options:
                print(
                    str(self.options.index(i)+1) + ") " + str(i)
                )
    
    class Servers(BaseMenu):
    
        def list_foo(self):
            pass
        def list_bar(self):
            pass
    
        options = (
            list_foo,
            list_bar
            )
    
        def __init__(self, menu_name):
            super(Servers, self).__init__(menu_name, self.options) 
    

    像这样实例化Servers 类:

    servers = Servers("Servers Menu")
    servers.display()
    

    输出:

    1) <function list_foo at 0x29e06e0>
    2) <function list_bar at 0x29e0758>
    

    合适吗?

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      • 2014-12-25
      相关资源
      最近更新 更多