【问题标题】:how to access parent class attribute in base class in python?如何在python中访问基类中的父类属性?
【发布时间】:2020-12-18 13:05:12
【问题描述】:

我是python开发的新手 我试图访问基类中的父类属性,但它给了我错误

Traceback(最近一次调用最后一次): 第 41 行,在 account.getAccountInfo() 第 24 行,在 getAccountInfo 中 print("ID" + str(self.cust.custID)) AttributeError: 'Account' 对象没有属性 'cust'

这是我的代码

class Customer:
    def __int__(self, custID=00000, cust_name="IJK", address="CABI", contact=9090909090):
        self.custID = custID
        self.cust_name = cust_name
        self.address = address
        self.contact = contact


class Account(Customer):
    def __int__(self, accountID=89839832389, balance=89999999.99):
        self.cust = super.__int__(self)
        self.accountID = accountID
        self.balance = balance

    def getAccountInfo(self):
        print("ID " + str(self.cust.custID))
        print("Name " + str(self.cust.cust_name))
        print("Account ID " + str(self.cust.accountID))

    def deposit(self, balance=2000, savings="true"):
        return self.balance + balance

    def withdraw(self, balance=500):
        return self.balance - balance

    def getBalance(self):
        return self.balance


account = Account()
account.getAccountInfo()
print("Actual balance " + str(account.getBalance()))
account.deposit()
print("Balance after deposit " + str(account.getBalance()))
account.withdraw()
print("Balance after withdraw " + str(account.getBalance()))

【问题讨论】:

  • 1) __int__ vs __init__ 2) self.cust = super.__int__(self) init 方法不返回任何内容 3) 不确定您是尝试继承还是只需要在 Account 中创建 Customer 对象
  • Himaprason 我必须做这件事创建一个从银行类继承的帐户类,具有以下属性(使用 Super() 将值传递给基类):

标签: python inheritance


【解决方案1】:

假设__int__ 应该是__init__ 试试这个:

class Customer:
    def __init__(self, custID=00000, cust_name="IJK", address="CABI", contact=9090909090):
        self.custID = custID
        self.cust_name = cust_name
        self.address = address
        self.contact = contact


class Account(Customer):
    def __init__(self, accountID=89839832389, balance=89999999.99):
        super().__init__()
        self.accountID = accountID
        self.balance = balance

    def getAccountInfo(self):
        print("ID " + str(self.custID))
        print("Name " + str(self.cust_name))
        print("Account ID " + str(self.accountID))

    def deposit(self, balance=2000, savings="true"):
        return self.balance + balance

    def withdraw(self, balance=500):
        return self.balance - balance

    def getBalance(self):
        return self.balance


account = Account()
account.getAccountInfo()
print("Actual balance " + str(account.getBalance()))
account.deposit()
print("Balance after deposit " + str(account.getBalance()))
account.withdraw()
print("Balance after withdraw " + str(account.getBalance()))

【讨论】:

  • 我收到此错误 print("ID" + str(self.custID)) AttributeError: 'Account' object has no attribute 'custID'
  • 能否分享导致错误的代码?
  • 我做了你改变的同样的事情
  • 使用的是哪个版本的python?我的作品低于 3.7
  • 我用的是3.9.1版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 2021-08-31
  • 1970-01-01
相关资源
最近更新 更多