python - python中一个类的提款方法

标签 python class methods

我正在做一些处理 Python 类的作业。但是我卡在了一个我不太明白的部分。

它告诉我:

--> 编写一个名为withdraw() 的方法,从帐户中提取指定金额。帐户中的余额减少方法参数中指定的金额。仅当参数中指定的金额小于或等于余额时,才应减少余额

这是我的程序

class Account:
    def __init__(self,id=0):
        self.__id = id
        self.__balance = 0
        self.__annualInterestRate = 0

    def getid(self):
        return self.__id

    def getbalance(self):
        return self.__balance

    def getannualInterestRate(self):
        return self.__getannualInterestRate

    def setid(self):
        self.__id = id

    def setbalance(self):
        self.__balance = balance

    def getMonthlyInterestRate(self):
        return self.__annualInterestRate/12


    def getMonthlyInterest(self):
        return self.__balance * getMonthlyInterestRate()

然后我将不得不:

def withdraw():
   # I don't know what to do here

最佳答案

您需要传递隐含的 self 参数(此处和其他一些方法)和 amount:

def withdraw(self, amount):
   # subtract amount from self.__balance

在继续上课之前,您还应该阅读 return 语句。

关于python - python中一个类的提款方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15807043/

相关文章:

C++:当不是所有的派生类都预先知道时使用继承?

ios - 移动到另一个 swift 文件时 UIAlertController 不显示

function - 对于良好的日常习惯来说,哪一个特征最重要?

python - 使用python对词袋模型进行简单的k均值聚类

c# - 预期的类、委托(delegate)、枚举、接口(interface)或结构

c++ - 在成员函数中返回 *this

java - 在 Java 中返回一个值

python - 你如何用每个列表中的 2 个元素制作字典?

python mysqldb 查询 with where

python - PostgreSQL 物化 View 未从 Python 刷新