You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

继承关系中从二维列表计算总值及扩展RegionalBank类实现区域总金额统计的技术问询

解决你的问题:扩展RegionalBank类 & 二维列表参数传递

一、正确传递二维列表到RegionalBank实例

其实你已经找对路子了!你的accounts变量本身就是标准的二维列表(每个子列表对应一家分支银行的账户存款数据),直接把它作为第三个参数传入RegionalBank的实例化语句就完全可行,就像你写的:

accounts = [ [10000, 13000, 22000], [30000, 7000, 19000], [15000, 23000, 31000] ]
RBank1 = RegionalBank("Regional Bank A", 132, accounts)

这个写法没问题——父类Bank的__init__方法接收accounts参数,子类RegionalBank通过super().__init__继承了这个逻辑,二维列表会被正确存储为实例的self.accounts属性。

不过注意你代码里这段循环有bug:

for row,col in accounts:
    print( accounts[rows][col] )

这里会抛出ValueError,因为accounts的每个元素是长度为3的列表,没法解包成row和col两个变量。如果要遍历所有账户值,可以改成:

# 遍历每个分支的账户列表,再遍历每个账户金额
for branch_accounts in accounts:
    for amount in branch_accounts:
        print(amount)

或者用索引遍历的方式:

for row_idx in range(len(accounts)):
    for col_idx in range(len(accounts[row_idx])):
        print(accounts[row_idx][col_idx])

二、实现正确的regional_total方法

你现在的regional_total方法只调用了父类的branch_total,但父类方法是计算单个分支的账户总和,我们需要的是所有分支的总存款。所以得遍历二维列表里的每个子列表(每个分支的账户数据),累加每个分支的总和:

推荐修改成更符合面向对象设计的写法——直接用实例自身的self.accounts,不用额外传参数:

class RegionalBank(Bank):
    def __init__(self, name, customers, accounts):
        super().__init__(name, customers, accounts)
    
    def regional_total(self):
        total = 0
        # 遍历每个分支的账户列表
        for branch_accounts in self.accounts:
            # 调用父方法计算单个分支总和,累加到总金额
            total += super().branch_total(branch_accounts)
        return total

调用的时候就简单多了:

total_deposit = RBank1.regional_total()
print(f"区域总存款金额: {total_deposit}")

如果需要支持传入自定义的二维列表计算,也可以调整方法保留参数:

def regional_total(self, accounts=None):
    # 没传参数就用实例自身的accounts,传了就用传入的
    target_accounts = accounts if accounts is not None else self.accounts
    total = 0
    for branch_accounts in target_accounts:
        total += super().branch_total(branch_accounts)
    return total

这样两种调用方式都支持:

# 使用实例自身存储的账户数据
RBank1.regional_total()
# 传入自定义的二维列表计算
custom_accounts = [[5000, 10000], [15000]]
RBank1.regional_total(custom_accounts)

完整修正后的代码

整合所有修改后的完整代码如下:

import sys
# 保留你从命令行参数获取数据的逻辑(如果需要的话)
strings = [l.split(",") for l in sys.argv[1].split("*")]
accounts_from_cmd = [[int(n) for n in s] for s in strings]

class Bank:
    def __init__(self, name, customers, accounts):
        self.name = name
        self.customers = customers
        self.accounts = accounts
    
    def branch_total(self, accounts):
        total = 0
        for account in accounts:
            total += account
        return total

# 测试用的二维账户列表
accounts = [ [10000, 13000, 22000], [30000, 7000, 19000], [15000, 23000, 31000] ]

class RegionalBank(Bank):
    def __init__(self, name, customers, accounts):
        super().__init__(name, customers, accounts)
    
    def regional_total(self, accounts=None):
        target_accounts = accounts if accounts is not None else self.accounts
        total = 0
        for branch_accounts in target_accounts:
            total += super().branch_total(branch_accounts)
        return total

# 正确遍历二维列表的示例
for branch_accounts in accounts:
    for amount in branch_accounts:
        print(f"账户金额: {amount}")

# 实例化区域银行并计算总存款
RBank1 = RegionalBank("Regional Bank A", 132, accounts)
total = RBank1.regional_total()
print(f"区域银行A的总存款: {total}")

内容的提问来源于stack exchange,提问作者Renzm.6958

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 22:22:28