【发布时间】:2021-11-23 09:56:55
【问题描述】:
在这个练习中,我不知道如何将数据从Person 导入到IndividualBankAccount。
在具体这一行:def __init__(self, sort_code: int, account_number: int, owner: Person):
我不确定这里发生了什么
super().__init__(sort_code,account_number)
如何将变量所有者:Person 传递给super()?
class Person:
_first_name : str
_second_name : str
_address : str
def __init__(self, fn: str, ln: str):
"creates a new person with first name fn last name ln and empty address"
self._first_name=fn
self.second_name=ln
class BankAccount:
_sort: int
_account_num: int
def __init__(self, sort_code: int, account_number: int)-> None:
'''creates a bank account with given sort code and account number'''
self._sort=sort_code
self._account_num=account_number
class IndividualBankAccount(BankAccount):
_owner: Person
def __init__(self, sort_code: int, account_number: int, owner: Person):
'''creates a new bank account with given sort code, account number, and owner'''
super().__init__(sort_code,account_number)
self._owner = owner
def get_account_data(self)-> str:
'''returns string "FN LN SC AN" where FN and LN are owner's first and last names,
SC is sort code, AN is account number'''
my_string =f"{XXXXwhat goes here???XXXX}"
return my_string
【问题讨论】:
-
你不会把它传递给
super().__init__,超类__init__只需要2个参数。 -
你可能只想要
self._owner = owner -
我已经更新了代码,希望现在更好
-
你了解过 f-strings/string interpolatioin 吗?任何字符串格式?甚至用
+连接字符串?很抱歉,这是一个完全不同的问题。 -
我不是在问如何格式化字符串,我是在问如何正确传递这些变量,我只发布了部分代码,在其他部分字符串格式化有效 my_string =f"{self ._sort} {self._account_num}" 如果我知道该怎么做,我就不会问了...
标签: python inheritance super