Dědictví cvičení import proměnných z jiné třídy

0

Otázka

V tomto cvičení nemohu přijít na to, jak importovat data z Person do IndividualBankAccount.

v konkrétních tento řádek: def __init__(self, sort_code: int, account_number: int, owner: Person):

Nejsem si jistý, co se děje tady

super().__init__(sort_code,account_number)

Jak mohu předat proměnné majitel: Personk 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
inheritance python super
2021-11-23 09:56:55
1

Nejlepší odpověď

0

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

2021-11-23 10:50:06

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................