繁体   English   中英

Python类继承函数并从子级传递参数

[英]Python Class Inheritance Function and Pass Parameters From Child

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
        )

尝试从“主菜单”->“服务器”子菜单开始创建一系列文本菜单。 当Servers()从BaseClass继承display()时,如何使继承的函数display()接收Servers()类中包含的选项元组和sub_menu =“ Server Menu”字符串?

您可以使用self.optionsself.sub_menudisplay功能,但是为什么你在引用它们的所有BaseMenu这些一无所知类optionssub_menu

第二个问题,您将"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继承而未定义optionssub_menu ,则在实例化时将导致TypeError ,如下所示:

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

我不确定我是否能完全理解您的要求,所以告诉我,这又如何呢?

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>

适合吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM