父类的初始化参数
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
复制代码
举报 使用道具
您需要登录后才可以回帖 登录 | 立即注册

精彩推荐

  • 网络爬虫采集列表数据
  • UMAP的min_dist参数有什么作用?
  • UMAP的n_neighbors参数有什么作用?
  • HDBSCAN的min_samples和min_cluster_size参
  • 为什么BERTopic话题缩减后特征词出现大量没

热门用户

GMT+8, 2026-6-18 21:13