父类的初始化参数
class BankAccount:
def __init__(self,name,account,balance):
self.name = name
self.account = account
self.balance = float(balance)
子类初始化参数
class IterestAccount(BankAccount):
def __init__(self,rate):
BankAccount.__init__(self)
self.rate = rate
令BalanceInYears = IterestAccount('ye','zhanghu',50,0.05)时
出现错误
BalanceInYears = IterestAccount('ye','zhanghu',50,0.05)
TypeError: __init__() takes exactly 2 arguments (5 given)
改成 BalanceInYears = IterestAccount(0.05)
出现
BankAccount.__init__(self)
TypeError: __init__() takes exactly 4 arguments (1 given)
心好累啊
举报 使用道具
| 回复

共 1 个关于本帖的回复 最后回复于 2016-9-7 10:39

沙发
ippfon 版主 发表于 2016-9-7 10:39:02 | 只看该作者
本帖最后由 ippfon 于 2016-9-8 00:15 编辑

子类显示调用父类的__init__方法问题,即BankAccount.__init__(self) 这句有问题,父类既然有初始化参数,子类调用父类的init方法应该也对应带上,应该更改为:
  1. def __init__(self,rate,name,account,balance):
  2.         BankAccount.__init__(self,name,account,balance)
  3.         self.rate = rate
复制代码
举报 使用道具
您需要登录后才可以回帖 登录 | 立即注册

精彩推荐

  • Gephi社会网络分析-马蜂窝游记文本分词并同
  • Gephi社会网络分析-基于马蜂窝游记文本以词
  • 知乎话题文本根据词语间距筛选后生成共词矩
  • 马蜂窝游记文本分词后以词语间距为筛选条件
  • 学习使用apriori算法挖掘关联关系

热门用户

GMT+8, 2024-4-19 19:32