如何在只有 self 参数的构造函数中设置属性。我也不知道如何定义没有实现的方法,里面一个class
How do I set properties in a constructor that has only a self argument. I also do not know how to define methods without implementation,Inside a class
这就是问题:它让我震惊;我的尝试是在我的下一个 post.
Create a class called bank account that has the methods withdraw and deposit with no implementation.
Create a class called savings account that inherits from bank account. SavingsAccount should have a constructor that only takes in a self argument. This constructor sets a property called balance to 500. (This should be the minimum balance at any given time).
In the savings account class, implement the deposit method that takes in cash deposit amounts, updates the balance accordingly and then returns the balance. For a negative deposit amounts, return invalid deposit amount. In the savings account class, implement the withdraw method that takes in the cash withdrawal amount, deducts this amount from the current balance and returns the balance. This method should never allow the balance to get below 500. (Check for this condition and output Cannot withdraw beyond the minimum account balance if it happens). Also, output Cannot withdraw beyond the current account balance if withdrawal amount is greater than current balance. For a negative withdrawal amount, return Invalid withdraw amount.
Create a class called current account that inherits from bank account. CurrentAccount should have a constructor that only takes in the self argument and sets a property called balance to 0.
In the current account class, implement a deposit method that takes in cash deposit amounts, updates the balance accordingly and then returns the balance. For a negative deposit amount, return invalid deposit amount. In the current account class, implement a withdraw method that takes in the cash withdrawal amount, deducts this amount from the current balance and returns the balance. For a negative withdrawal amount, return invalid withdraw amount. Withdrawing more than the current balance should fail with message cannot withdraw beyond the current account balance.
这是我的尝试:
class BankAccount:
def __init__
(self,name,number,balance):
self.name=name
self.number=number
self.balance=balance
class SavingsAccount(BankAccount):
def __init__(self, balance=500):
assert(self.balance>500), "minimum balance is 500"
def deposit (self, amount):
if amount<0:
return "Invalid deposit amount"
self.balance+=amount
return self.balance
def withdraw(self,amount):
Assert
(self.balance>amount),
"cannot withdraw"
if amount<0:
return "Invalid amount"
self.balance-=amount
return self.balance
class currentaccount(BankAccount):
def __init__(self,balance=0):
def deposit (self, amount):
if amount<0:
return ("Invalid deposit amount")
self.balance+=amount
return self.balance
def withdrawal (self, amount):
assert (self.balance > amount),"cannot withdraw beyond the current account balance"
if amount<0:
return "Invalid amount"
self.balance-=amount
return self.balance
仔细阅读作业:你每次都将余额设置为500;不需要另一个构造函数参数。没有名字,没有账号。
"no implementation"方法很简单:
def method():
pass
这让你动起来了吗?不要试图让它变得比实际更难:从简单开始,一次写几行,并在继续之前调试它们。
下面的代码有效!
class BankAccount(object):
def __init__(self):
pass
def withdraw():
pass
def deposit():
pass
class SavingsAccount(BankAccount):
def __init__(self):
self.balance = 500
def deposit(self, amount):
if (amount < 0):
return "Invalid deposit amount"
else:
self.balance += amount
return self.balance
def withdraw(self, amount):
if ((self.balance - amount) > 0) and ((self.balance - amount) < 500):
return "Cannot withdraw beyond the minimum account balance"
elif (self.balance - amount) < 0:
return "Cannot withdraw beyond the current account balance"
elif amount < 0:
return "Invalid withdraw amount"
else:
self.balance -= amount
return self.balance
class CurrentAccount(BankAccount):
def __init__(self):
self.balance = 0
def deposit(self, amount):
if amount < 0:
return "Invalid deposit amount"
else:
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount < 0:
return "Invalid withdraw amount"
elif self.balance < amount:
return "Cannot withdraw beyond the current account balance"
else:
self.balance -= amount
return self.balance
要设置仅包含 self 参数的构造函数,请执行此操作
class Example(object):
def __init__(self):
pass
对于这个特定的问题,请逐步阅读说明并按照说明执行方法。这里的问题是逻辑正确。 在储蓄账户 class 中,确保首先检查透支,然后检查提款不会导致余额低于 500。 这个顺序很重要。我遇到了类似的问题,并在进行更改后解决了它。
解决方法如下:
class BankAccount(object):
def withdraw():
pass
def deposit():
pass
class SavingsAccount(BankAccount):
def __init__(self):
self.balance = 500
def deposit(self, amount):
if amount < 0:
return "Invalid deposit amount"
else:
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount < 0:
return "Invalid withdraw amount"
elif amount > self.balance:
return "Cannot withdraw beyond the current account balance"
elif (self.balance - amount) < 500:
return "Cannot withdraw beyond the minimum account balance"
else:
self.balance -= amount
return self.balance
class CurrentAccount(BankAccount):
def __init__(self):
self.balance = 0
def deposit(self, amount):
if amount < 0:
return "Invalid deposit amount"
else:
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount < 0:
return "Invalid withdraw amount"
elif self.balance < amount:
return "Cannot withdraw beyond the current account balance"
else:
self.balance -= amount
这就是问题:它让我震惊;我的尝试是在我的下一个 post.
Create a class called bank account that has the methods withdraw and deposit with no implementation.
Create a class called savings account that inherits from bank account. SavingsAccount should have a constructor that only takes in a self argument. This constructor sets a property called balance to 500. (This should be the minimum balance at any given time).
In the savings account class, implement the deposit method that takes in cash deposit amounts, updates the balance accordingly and then returns the balance. For a negative deposit amounts, return invalid deposit amount. In the savings account class, implement the withdraw method that takes in the cash withdrawal amount, deducts this amount from the current balance and returns the balance. This method should never allow the balance to get below 500. (Check for this condition and output Cannot withdraw beyond the minimum account balance if it happens). Also, output Cannot withdraw beyond the current account balance if withdrawal amount is greater than current balance. For a negative withdrawal amount, return Invalid withdraw amount.
Create a class called current account that inherits from bank account. CurrentAccount should have a constructor that only takes in the self argument and sets a property called balance to 0.
In the current account class, implement a deposit method that takes in cash deposit amounts, updates the balance accordingly and then returns the balance. For a negative deposit amount, return invalid deposit amount. In the current account class, implement a withdraw method that takes in the cash withdrawal amount, deducts this amount from the current balance and returns the balance. For a negative withdrawal amount, return invalid withdraw amount. Withdrawing more than the current balance should fail with message cannot withdraw beyond the current account balance.
这是我的尝试:
class BankAccount:
def __init__
(self,name,number,balance):
self.name=name
self.number=number
self.balance=balance
class SavingsAccount(BankAccount):
def __init__(self, balance=500):
assert(self.balance>500), "minimum balance is 500"
def deposit (self, amount):
if amount<0:
return "Invalid deposit amount"
self.balance+=amount
return self.balance
def withdraw(self,amount):
Assert
(self.balance>amount),
"cannot withdraw"
if amount<0:
return "Invalid amount"
self.balance-=amount
return self.balance
class currentaccount(BankAccount):
def __init__(self,balance=0):
def deposit (self, amount):
if amount<0:
return ("Invalid deposit amount")
self.balance+=amount
return self.balance
def withdrawal (self, amount):
assert (self.balance > amount),"cannot withdraw beyond the current account balance"
if amount<0:
return "Invalid amount"
self.balance-=amount
return self.balance
仔细阅读作业:你每次都将余额设置为500;不需要另一个构造函数参数。没有名字,没有账号。
"no implementation"方法很简单:
def method():
pass
这让你动起来了吗?不要试图让它变得比实际更难:从简单开始,一次写几行,并在继续之前调试它们。
下面的代码有效!
class BankAccount(object):
def __init__(self):
pass
def withdraw():
pass
def deposit():
pass
class SavingsAccount(BankAccount):
def __init__(self):
self.balance = 500
def deposit(self, amount):
if (amount < 0):
return "Invalid deposit amount"
else:
self.balance += amount
return self.balance
def withdraw(self, amount):
if ((self.balance - amount) > 0) and ((self.balance - amount) < 500):
return "Cannot withdraw beyond the minimum account balance"
elif (self.balance - amount) < 0:
return "Cannot withdraw beyond the current account balance"
elif amount < 0:
return "Invalid withdraw amount"
else:
self.balance -= amount
return self.balance
class CurrentAccount(BankAccount):
def __init__(self):
self.balance = 0
def deposit(self, amount):
if amount < 0:
return "Invalid deposit amount"
else:
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount < 0:
return "Invalid withdraw amount"
elif self.balance < amount:
return "Cannot withdraw beyond the current account balance"
else:
self.balance -= amount
return self.balance
要设置仅包含 self 参数的构造函数,请执行此操作
class Example(object):
def __init__(self):
pass
对于这个特定的问题,请逐步阅读说明并按照说明执行方法。这里的问题是逻辑正确。 在储蓄账户 class 中,确保首先检查透支,然后检查提款不会导致余额低于 500。 这个顺序很重要。我遇到了类似的问题,并在进行更改后解决了它。
解决方法如下:
class BankAccount(object):
def withdraw():
pass
def deposit():
pass
class SavingsAccount(BankAccount):
def __init__(self):
self.balance = 500
def deposit(self, amount):
if amount < 0:
return "Invalid deposit amount"
else:
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount < 0:
return "Invalid withdraw amount"
elif amount > self.balance:
return "Cannot withdraw beyond the current account balance"
elif (self.balance - amount) < 500:
return "Cannot withdraw beyond the minimum account balance"
else:
self.balance -= amount
return self.balance
class CurrentAccount(BankAccount):
def __init__(self):
self.balance = 0
def deposit(self, amount):
if amount < 0:
return "Invalid deposit amount"
else:
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount < 0:
return "Invalid withdraw amount"
elif self.balance < amount:
return "Cannot withdraw beyond the current account balance"
else:
self.balance -= amount