简体   繁体   English

如何在子类中创建的字典中调用父类的方法?

[英]How do you call a parent class's method within a dictionary created in the child class?

I'm working on a project about inheritance and bank accounts, where I define a parent and child class (Account and Checking_Account respectively).我正在做一个关于继承和银行账户的项目,在那里我定义了一个父类和一个子类(分别是 Account 和 Checking_Account)。

Checking_Account contains a static dictionary that maps strings to functions called "options". Checking_Account 包含一个静态字典,将字符串映射到称为“选项”的函数。 Some of the functions are defined in the child class Checking_Account, some are defined in the parent class Account.一些函数在子类Checking_Account 中定义,一些在父类Account 中定义。

For example,例如,

options = {"See Number of Remaining Checks" : _remainingChecks,
           "View Balance" : super()._viewBalance}

However, this returns the error但是,这会返回错误

Traceback (most recent call last):
  File "/Users/dawsonren/Documents/ASWDV/Python/Accounting/account_SO.py", line 24, in <module>
    class Checking_Account(Account):
  File "/Users/dawsonren/Documents/ASWDV/Python/Accounting/account_SO.py", line 33, in Checking_Account
    "View Balance" : super()._viewBalance()}
RuntimeError: super(): no arguments

Here's a minimum reproductable example.这是一个最小的可复制示例。

class Account(object):
    def __init__(self, name, bal):
        """(str, float) => Account object

        Instantiates the Account object.

        """
        self.name = name
        self.balance = bal

    def viewBalance(self):
        print(f"Account has ${self.balance}.")

    def run(self, options):
        """{str : function} => None
        Runs the function based on the input of the user.
        """

        for key in options.keys():
            print(key)
        select = input("Type the option you want.")
        options[select]()

class Checking_Account(Account):
    def __init__(self, name, bal, checks = 100):
        super().__init__(name, bal)
        self.checks = checks

    def remainingChecks(self):
        print(f"Account {self.num} has {self.checks} checks.")

    options = {"See Number of Remaining Checks" : remainingChecks,
               "View Balance" : super().viewBalance}

    def run(self):
        super().run(Checking_Account.options)

This is my first post here as a high schooler, so I could use any help I can get.这是我作为高中生的第一篇文章,所以我可以使用我能得到的任何帮助。 If what I'm doing isn't best practice, please point me to the right way to do it!如果我所做的不是最佳实践,请指出正确的方法!

I suppose you had classes about inheritance in Object Oriented Programmation, or at least have read about it?我想你在面向对象编程中有关于继承的课程,或者至少读过它?

Here, your class Checking_Account inherits from Account , so this means that it inherits, among other things, of its methods.在这里,您的类Checking_Account继承自Account ,因此这意味着它继承了其方法等。

So no need to try to call super().viewBalance , you just have to call self.viewBalance() .所以不需要尝试调用super().viewBalance ,你只需要调用self.viewBalance()

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

相关问题 如何在父类方法中调用子类方法? - How do I call a Child class method from within a Parent class Method? 如何从 Python 中的子类调用父类的方法? - How do I call a parent class's method from a child class in Python? add 方法已经在基类中创建。 我如何从带有放置值的子类中调用它 - The add method has already created within the base class. How do I call it from a child class with putting values 如何调用子 class 而不是父 class 方法的覆盖方法? - How do I call the overwritten method of the child class instead of the parent class method? 如何在Python的子类中调用和覆盖父类方法 - How to call and override a Parent class method from Child class in Python Python父类init调用子级的覆盖方法 - Python parent class init call child's overwritten method 在类中调用parent的__call__方法 - Calling parent's __call__ method within class Python:如何使用父 class 中的方法装饰子类中的方法? - Python: How do you decorate methods in child classes using a method in the parent class? python:在父类的方法中,调用该类的类方法,但绑定到子类 - python: in parent class's method, call classmethod of that class, but bound to a child class Python 如何从父元类调用子 class 方法 - Python How to call child class method from Parent metaclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM