设计一个名为 class 的帐户并进行测试
Design a class named Account and test it
好吧,我在编写这段代码时遇到了问题,所用的测试代码是,无论我尝试什么,我似乎都无法让它工作。
有问题的作业说:设计一个 class 命名帐户,其中包含:
■ 一个名为 id 的私有整数数据字段。
■ 帐户的名为 balance 的私有浮动数据字段。
■ 名为 annualInterestRate 的私有浮动数据字段,用于存储当前利率
利率。
■ 一个创建具有指定id(默认为0)的帐户的构造函数,initial
余额(默认 100)和年利率(默认 0)。
■ id、balance 和 annualInterestRate 的访问器和修改器方法。
■ 名为 getMonthlyInterestRate() 的方法 return 是每月
利率。
■ 名为 getMonthlyInterest() 的方法,return 是每月利息。
■ 一个名为 withdraw 的方法,它从
帐号。
■ 一个名为 deposit 的方法,将指定金额存入帐户。
(提示:方法getMonthlyInterest()是return每月的利息金额,不是
利率。使用此公式计算每月利息:余额 *
月利率。 monthlyInterestRate 是 annualInterestRate
/ 12. 注意 annualInterestRate 是一个百分比(比如 4.5%)。你需要
除以 100。)
编写一个测试程序,创建一个帐户对象,帐户 ID 为 1122,一个
余额为 20,000 美元,年利率为 4.5%。使用取款
方法提取2500美元,使用存款方法存入3000美元,并打印
id、余额、月利率、月利息
这是我目前为 class:
编写的代码
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 我只是把它们放在那里作为占位符。
这是我的测试程序(因为我迷路了,目前还不完整):
from Account import Account
def main():
account1 = Account()
print("Account id # is", account1.getid)
print("Beginning Balance: ", account1.setbalance)
print("Monthly Interest Rate: ", account1.getMonthlyInterestRate())
print("Monthly Interest: ", account1.getMonthlyInterest())
main()
我不知道我在做什么。如果有人知道我应该做什么,请帮忙。
我正在尝试弄清楚我如何将信息输入 class(或者测试程序,如果需要的话)并让它输出如下内容:
帐户 ID # 是,1122
期初余额:提款后的余额 and/or 存款
月利率:无论所有数学结果如何。
每月利息:同样,不管所有的数学结果如何
在开始谈论 class 的功能之前,我先谈谈 class 的设计。
■ 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.
你可能没有注意到,但你实际上是在制作你的数据字段public。考虑以下代码:
class Account:
id = 2
id 可以从代码中的任何其他地方访问,例如(请注意,这不是使用 class 的实例):
print(Account.id)
继续这个例子,我们可以让一个实例有不同的 id 值。
class account:
id = 2
def __init__(self):
self.id = 4
print(account.id)
foo = account()
print(foo.id)
这个id仍然可以从class的实例外部访问,它仍然是public。为了使其私有,我们在变量前添加一个 __ 。现在我们应该在尝试访问实例的 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
如果此时您仍然感到困惑,我建议您阅读有关 "access specifiers" 和 "encapsulation" 的内容。如果你用 Java 或 C++ 等不同的语言阅读它也没关系,它是完全相同的概念。
此外,我会在构造函数中将 accountid
转换为 int,将 initialbalance
和 annualInterestRate
转换为浮点数,这样您就不必将其他 "getter" 方法(那些只是 returning 变量)。无论如何,它们应该保存为那些数据类型,因为这些是要求。
下一个要点指出(跳过构造函数,你得到了那个):
■ 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.
现在 class 设计已经完成,我们需要处理 class 功能。我们将通过 return 变量来减少 "getter" (访问器)方法中的代码,因为它已经是正确的数据类型。现在我们只需要创建 "setter" (mutator) 方法,这将改变值。看你的代码,我认为你对getter和setter方法没有完全理解,我将简要描述它们。
一个getter方法只是return一个class变量,否则是隐藏的(因为它是私有的)
A setter 方法只是更改 class 变量的值。您需要通过一种方法来执行此操作,因为该变量是私有的。我们将为 setter 方法使用一个参数,它不会 return 任何东西。
class 现在应该看起来像这样
'''
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
你的getmonthlyInterestRate()方法应该是/12,最后,monthlyInterestRate没有定义在getMonthlyInterest()的范围内。将其替换为 self.getMonthlyInterestRate()。测试时,请确保使用参数
初始化您的帐户对象
最后,这里是完整的 class 代码:
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()
我认为你应该多研究一下函数,如何使用它们来简化你的代码,class 访问说明符,这样你就可以理解最小权限原则。我知道现在这可能没有任何意义,但通过一些 google 搜索和尝试一些代码,一切都会变得有意义。
好吧,我在编写这段代码时遇到了问题,所用的测试代码是,无论我尝试什么,我似乎都无法让它工作。
有问题的作业说:设计一个 class 命名帐户,其中包含:
■ 一个名为 id 的私有整数数据字段。
■ 帐户的名为 balance 的私有浮动数据字段。
■ 名为 annualInterestRate 的私有浮动数据字段,用于存储当前利率 利率。
■ 一个创建具有指定id(默认为0)的帐户的构造函数,initial 余额(默认 100)和年利率(默认 0)。
■ id、balance 和 annualInterestRate 的访问器和修改器方法。
■ 名为 getMonthlyInterestRate() 的方法 return 是每月 利率。
■ 名为 getMonthlyInterest() 的方法,return 是每月利息。
■ 一个名为 withdraw 的方法,它从 帐号。
■ 一个名为 deposit 的方法,将指定金额存入帐户。
(提示:方法getMonthlyInterest()是return每月的利息金额,不是 利率。使用此公式计算每月利息:余额 * 月利率。 monthlyInterestRate 是 annualInterestRate / 12. 注意 annualInterestRate 是一个百分比(比如 4.5%)。你需要 除以 100。)
编写一个测试程序,创建一个帐户对象,帐户 ID 为 1122,一个 余额为 20,000 美元,年利率为 4.5%。使用取款 方法提取2500美元,使用存款方法存入3000美元,并打印 id、余额、月利率、月利息
这是我目前为 class:
编写的代码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 我只是把它们放在那里作为占位符。
这是我的测试程序(因为我迷路了,目前还不完整):
from Account import Account
def main():
account1 = Account()
print("Account id # is", account1.getid)
print("Beginning Balance: ", account1.setbalance)
print("Monthly Interest Rate: ", account1.getMonthlyInterestRate())
print("Monthly Interest: ", account1.getMonthlyInterest())
main()
我不知道我在做什么。如果有人知道我应该做什么,请帮忙。
我正在尝试弄清楚我如何将信息输入 class(或者测试程序,如果需要的话)并让它输出如下内容:
帐户 ID # 是,1122
期初余额:提款后的余额 and/or 存款
月利率:无论所有数学结果如何。
每月利息:同样,不管所有的数学结果如何
在开始谈论 class 的功能之前,我先谈谈 class 的设计。
■ 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.
你可能没有注意到,但你实际上是在制作你的数据字段public。考虑以下代码:
class Account:
id = 2
id 可以从代码中的任何其他地方访问,例如(请注意,这不是使用 class 的实例):
print(Account.id)
继续这个例子,我们可以让一个实例有不同的 id 值。
class account:
id = 2
def __init__(self):
self.id = 4
print(account.id)
foo = account()
print(foo.id)
这个id仍然可以从class的实例外部访问,它仍然是public。为了使其私有,我们在变量前添加一个 __ 。现在我们应该在尝试访问实例的 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
如果此时您仍然感到困惑,我建议您阅读有关 "access specifiers" 和 "encapsulation" 的内容。如果你用 Java 或 C++ 等不同的语言阅读它也没关系,它是完全相同的概念。
此外,我会在构造函数中将 accountid
转换为 int,将 initialbalance
和 annualInterestRate
转换为浮点数,这样您就不必将其他 "getter" 方法(那些只是 returning 变量)。无论如何,它们应该保存为那些数据类型,因为这些是要求。
下一个要点指出(跳过构造函数,你得到了那个):
■ 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.
现在 class 设计已经完成,我们需要处理 class 功能。我们将通过 return 变量来减少 "getter" (访问器)方法中的代码,因为它已经是正确的数据类型。现在我们只需要创建 "setter" (mutator) 方法,这将改变值。看你的代码,我认为你对getter和setter方法没有完全理解,我将简要描述它们。
一个getter方法只是return一个class变量,否则是隐藏的(因为它是私有的)
A setter 方法只是更改 class 变量的值。您需要通过一种方法来执行此操作,因为该变量是私有的。我们将为 setter 方法使用一个参数,它不会 return 任何东西。
class 现在应该看起来像这样
'''
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
你的getmonthlyInterestRate()方法应该是/12,最后,monthlyInterestRate没有定义在getMonthlyInterest()的范围内。将其替换为 self.getMonthlyInterestRate()。测试时,请确保使用参数
初始化您的帐户对象最后,这里是完整的 class 代码:
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()
我认为你应该多研究一下函数,如何使用它们来简化你的代码,class 访问说明符,这样你就可以理解最小权限原则。我知道现在这可能没有任何意义,但通过一些 google 搜索和尝试一些代码,一切都会变得有意义。