python - 在 Python 3 中创建一个 Account 类

标签 python class python-3.x

我想知道是否有人可以帮我解决这个问题?我正在使用 python 3,我希望这样做。但我已经无可救药地迷失了。任何帮助将不胜感激!

Account 类应包含以下内容 帐户的私有(private) int 数据字段,名为 id,默认值为 0。

帐户的名为余额的私有(private) double 据字段,默认值为 0。

名为annualInterestRate的私有(private) double 据字段,存储当前利率,默认值为0。假设所有账户的利率相同。

名为 dateCreated 的私有(private)日期数据字段,用于存储创建帐户的日期。

创建默认帐户的无参构造函数。

一个构造函数,用于创建具有指定 ID 和初始余额的帐户。

id、balance 和 AnnualInterestRate 的访问器和修改器方法。

dateCreated 的访问器方法。

名为 getMonthlyInterestRate() 的方法,返回月利率。

名为withdraw的方法,可从帐户中提取指定金额。

一种名为 Deposit 的方法,将指定金额存入帐户。

实现该类。编写一个测试程序,创建一个帐户对象,帐户 ID 为 1122,余额为 20,000 美元,年利率为 4.5%。使用提款方法提取 2,500 美元,使用存款方法存入 3,000 美元,并打印余额、月利息和创建此帐户的日期。

我现在的代码是:

class Account:

    def __init__(self):
        self.balance=balance

    def getMonthlyInterestRate(self):
        return (annualInterest/100)

    def getMonthlyInterest(self):
        return blanace * monthlyInterestRate

现在我拥有的其他代码是:

from Account import Account

def main():

    account = Account()
    account.getMonthlyInterestRate(4.5)

    print("Beginning Balance: ", account.balance)
    print("Monthly Interest Rate: ", account.getMonthlyInterestRate)
    print("Monthly Interest: ", account.getMonthlyInterest)

main()

最佳答案

只需将您的类修改为这样:

 class Account:

    def __init__(self, balance, annual_interest_rate):
        self.balance=balance
        self.annualInterest = annual_interest_rate

    def getMonthlyInterestRate(self):
        return (self.annualInterest/100)

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

请注意,我已将余额和年利率作为构造函数参数传递 因此要实例化一个帐户对象:

account = Account(200, 5)

关于python - 在 Python 3 中创建一个 Account 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19532578/

相关文章:

c++ - 错误: Cannot synthesize a Constructor for A

python tkinter 通过单击按钮打开新窗口并关闭第一个窗口

python - 获取文件的最后n行——解码错误

python - 我怎么知道我的 python 脚本是否正在运行? (使用 Cygwin 或 Windows shell )

python - 在 C 程序中嵌入 Python 模块时,如何将 C 结构传递到 Python 中

delphi - delphi中的单位与其他语言中的类相同吗?

c++ - 如何在类,c++中使用带参数的函数?

python - 多处理值因锁定而挂起

python - 如何更改图像上像素部分的 Alpha channel ?

python - 在Python中使用正则表达式解析具有重复模式的字符串?