简单类 - >类属性更新

时间:2017-08-22 12:57:08

标签: python class

我有一个简单的课程。这是我得到的输出:

>>> print(Customer.total_amount)
1300

但我希望输出为:

>>> print(Customer.total_amount)
1000

我做错了什么?

class Customer:
    total_amount = 0

    def __init__(self, name, mob, email, amount=None):
        self.name = name
        self.mob = mob
        self.eamil = email
        self.amount = 0

    def add_amount(self, amount):
        self.amount += amount
        Customer.total_amount += self.amount

cust1 = Customer("cust1", "8892398598", "ritheshb1@gmail.com")
cust2 = Customer("cust2", "8892498598", "ritheshb2@gmail.com")
cust1.add_amount(100)
cust2.add_amount(200)
cust1.add_amount(300)
cust2.add_amount(400)

print(cust1.amount)
print(cust2.amount)
print(Customer.total_amount)

1 个答案:

答案 0 :(得分:1)

更改:

Customer.total_amount += self.amount

Customer.total_amount += amount