设计一个名为Account和testi的类

2024-04-26 11:40:09 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,所以我在编写这段代码时遇到了一个问题,用于is的测试代码,不管我做了什么,我似乎都不能让它正常工作。在

有问题的作业是:设计一个名为Account的类,它包含:

■账户的一个名为id的私有int数据字段。在

■一个名为账户余额的私有浮动数据字段。在

■一个名为annualInterestate的私有浮点数据字段,用于存储当前 利率。在

■一个构造函数,用指定的id(默认为0)创建一个帐户,初始 余额(默认为100)和年利率(默认为0)。在

■id、balance和AnnualInterestate的访问器和变异器方法。在

■一个名为getMonthlyInterestRate()的方法,它返回每月 利率。在

■一个名为getMonthlyInterest()的方法,返回每月利息。在

■一种名为“提取”的方法,从 帐户。在

■一种名为“存款”的方法,将指定金额存入账户。在

(提示:getMonthlyInterest()方法是返回每月利息金额,而不是 利率。使用此公式计算月利息:余额* 每月战略。每月利息是年度利息 /12。注意,annualInterestate是一个百分比(比如4.5%)。你需要 除以100。)

编写一个测试程序,创建一个帐户id为1122的Account对象 余额20000美元,年利率4.5%。使用取款机 方法提取2500美元,使用存款方法存入3000美元,然后打印 id、余额、月利率和月利息。在

这是我到目前为止的课程代码:

class Account:

    def __init__(self, accountid = 0, initialbalance = 100, annualInterestrate = 0):
        self.accountid = accountid
        self.initalbalance = initialbalance
        self.annualInterestrate = annualInterestrate

    def balance(self):
        return float(self.balance)

    def id(self):
        return int(self.id)

    def annualInterestrate(self):
        return float(self.annaulInterestrate)

    def getid(self):
        return self.id

    def setbalance(self):
        return self.setbalance 

    def getannualInterestrate(self):
        return self.annualInterestrate

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

    def withdraw(self):
        amount = 28
        if self.balance>=amount: 
            self.balance-=amount 

    def deposit(self): 
        amount =  45
        self.balance += amount 

    def getMonthlyInterest(self):
        return  balance * monthlyInterestRate. monthlyInterestRate is annualInterestRate / 12

忽略数字45和28我只是把它们作为占位符。在

这是我为测试程序准备的(由于我迷路了,目前还不完整):

^{pr2}$

我不知道我在做什么。如果有人知道我该怎么做,请帮忙。在

我试图弄清楚我是如何将信息输入到类中的(如果需要的话,也可以是测试程序),并让它输出如下内容:

账户id为1122

期初余额:取现和/或存款后的余额

月利率:不管数学结果如何。在

月利息:再说一遍,不管数学结果如何


Tags: 方法selfidreturndef帐户账户account
1条回答
网友
1楼 · 发布于 2024-04-26 11:40:09

在我开始讨论类的功能之前,我将讨论一下类的设计。在

■ A private int data field named id for the account.

■ A private float data field named balance for the account.

■ A private float data field named annualInterestRate that stores the current interest rate.

您可能没有注意到这一点,但实际上您是在公开您的数据字段。考虑以下代码:

class Account:
   id = 2

可以从代码中的任何其他位置访问该id,例如(请注意,这并没有使用类的实例):

^{pr2}$

继续这个例子,我们可以让一个实例具有不同的id值

^{3}$

这个id仍然可以从类的实例外部访问,它仍然是公共的。为了使其私有化,我们在变量前面添加一个∗。现在我们在尝试访问实例的id时应该会得到一个错误,但是当函数访问的是id时则不会

class account:
  def __init__(self):
    self.__id = 4

  def getId(self):
    return self.__id

foo = account()
print(foo.getId())
print(foo.__id) # Will produce an error

如果您在这一点上仍然感到困惑,我建议您阅读“访问说明符”和“封装”。如果你用java或C++语言来阅读它,那就是完全相同的概念。在

另外,我会将accountid转换为int,并将initialbalance和{}转换为构造函数中的float,因此您不必强制转换其他“getter”方法(那些只返回变量的方法)。无论如何,它们都应该保存为那些数据类型,因为这是需求。在

下一个要点指出(跳过构造器一个,你得到了那个):

■ The accessor and mutator methods for id, balance, and annualInterestRate.

■ A method named getMonthlyInterestRate() that returns the monthly interest rate.

■ A method named getMonthlyInterest() that returns the monthly interest.

■ A method named withdraw that withdraws a specified amount from the account.

■ A method named deposit that deposits a specified amount to the account.

现在类设计已经完成,我们需要处理类函数。我们将通过返回变量来减少“getter”(accessor)方法中的代码,因为它已经是正确的数据类型。现在我们只需要创建“setter”(mutator)方法,它将更改值。看看你的代码,我认为你对getter和setter方法没有完全的理解,我将简要地描述它们。在

getter方法只返回一个以其他方式隐藏的类变量(因为它是私有的)

setter方法只是更改类变量的值。因为变量是私有的,所以需要通过一个方法来完成。我们将为setter方法使用一个参数,它不会返回任何内容。在

这个类现在应该是这样的

'''
Random Project
       
This is what I use for testing things
'''
class account:
  # Constructor
  def __init__(self, id = 0, balance = 100.0, annualInterestRate = 0.0):
    self.__id = int(id)
    self.__balance = float(balance)
    self.__annualInterestRate = float(annualInterestRate)

  # Getters (Accessors)
  def getId(self):
    return self.__id

  def getBalance(self):
    return self.__balance

  def getAnnualInterestRate(self):
    return self.__annualInterestRate

  # Setters (Mutators)
  '''
  Notice that they are the exact
  same as the constructor, just
  that they edit the variables
  individually
  '''
  def setId(self, id):
    self.__id = int(id)

  def setBalance(self, balance):
    self.__balance = float(balance)

  def setAnnualInterestRate(self, annualInterestRate):
    self.__annualInterestRate = float(annualInterestRate)

最后一点是:

■ A method named getMonthlyInterestRate() that returns the monthly interest rate.

■ A method named getMonthlyInterest() that returns the monthly interest.

■ A method named withdraw that withdraws a specified amount from the account.

■ A method named deposit that deposits a specified amount to the account.

您的取款和存款功能很好,但是您应该使用参数

def withdraw(self, amount):
   self.__balance -= amount

def deposit(self, amount):
   self.__balance += amount

get-MonthlyInterestate()方法应为/12,最后,在getMonthlyInterest()的作用域中未定义MonthlyInterest()。替换为self.getMonthlyInterestate(). 测试时,请确保使用参数初始化帐户对象

最后,这是整个类代码:

class account:
  # Constructor
  def __init__(self, id = 0, balance = 100.0, annualInterestRate = 0.0):
    self.__id = int(id)
    self.__balance = float(balance)
    self.__annualInterestRate = float(annualInterestRate)

  # Getters (Accessors)
  def getId(self):
    return self.__id

  def getBalance(self):
    return self.__balance

  def getAnnualInterestRate(self):
    return self.__annualInterestRate

  # Setters (Mutators)
  '''
  Notice that they are the exact
  same as the constructor, just
  that they edit the variables
  individually
  '''
  def setId(self, id):
    self.__id = int(id)

  def setBalance(self, balance):
    self.__balance = float(balance)

  def setAnnualInterestRate(self, annualInterestRate):
    self.__annualInterestRate = float(annualInterestRate)

  # Calculating functions
  def getMonthlyInterestRate(self):
    return self.__annualInterestRate / 12

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

我认为你应该多研究一下函数,如何使用它们来简化代码,类访问说明符,这样你就能理解最小特权原则。我知道现在这可能没有意义,但是通过一些谷歌搜索和一些代码,一切都会有意义的。在

相关问题 更多 >