【问题标题】:Inheritance exercise import variables from another class继承练习从另一个类导入变量
【发布时间】: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


【解决方案1】:

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
  
  def set_address(self, adr: str)-> None:
    "sets the address to adr"
    self._address=adr

  def get_address(self)-> str:
    "returns the address"
    return self._address

    
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
    
    
  def set_sort_code(self, sort_code: int)-> None:
    '''updates sort code'''
    self._sort=sort_code
        
  def get_sort_code(self)-> int:
    '''returns sort code'''
    return int(self._sort)
        
  def set_account_number(self, account_number: int)-> None:
    '''updates account number'''
    self._account_num=account_number
    
  def get_account_number(self)-> int:
    '''returns account number'''
    return int(self._account_num)
    
  def get_account_data(self)-> str:
    '''returns string "SC AN" where SC is sort code, AN is account number'''
    my_string =f"{self._sort} {self._account_num}"
    return my_string

    
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
    self.sort_code=sort_code
    self.account_number=account_number
        
  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"{self._owner._first_name} {self._owner.second_name} {self.sort_code} {self.account_number}"
    return my_string

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多