Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将继承添加到银行帐户类?_Python_Oop_Inheritance - Fatal编程技术网

Python 如何将继承添加到银行帐户类?

Python 如何将继承添加到银行帐户类?,python,oop,inheritance,Python,Oop,Inheritance,我目前正在试验OOP,我对它非常陌生,在将继承应用于代码时遇到了麻烦,我理解它是一种由另一个类获取一个类的特性和行为的机制,请给出您的见解 from random import randint import time class BankAccount(object): def __init__(self, initial_balance=0): self.balance = initial_balance def deposit(self, amount):

我目前正在试验OOP,我对它非常陌生,在将继承应用于代码时遇到了麻烦,我理解它是一种由另一个类获取一个类的特性和行为的机制,请给出您的见解

from random import randint
import time

class BankAccount(object):
    def __init__(self, initial_balance=0):
        self.balance = initial_balance
    def deposit(self, amount):
        self.balance += amount
    def withdraw(self, amount):
        self.balance -= amount
    def overdrawn(self):
        return self.balance < 0

balance = (randint(100, 500))
my_account = BankAccount(balance)
my_account.balance

variable = 0
print("Welcome to Your Bank Account")
time.sleep(1)

def main():
    while variable == 0:
        time.sleep(2)
        print("Your current Balance is", my_account.balance , "pounds")
        time.sleep(0.5)
        print("Would you like to deposit or withdraw money")

    decision = input("write 'deposit' or 'withdraw', is case-sensitive: ")
    if decision == "deposit":
        depositAmmount = float(input("How much would you like to deposit: "))
        my_account.deposit(depositAmmount)
        print("Your new balance is" , round(my_account.balance, 2))
    elif decision == "withdraw":
        withdrawAmmount = float(input("How much would you like to withdraw: "))
        my_account.withdraw(withdrawAmmount)
        print("Your new balance is" , round(my_account.balance, 2))
        if my_account.balance < 0:
            time.sleep(1)
            print("Your in an OVERDRAFT of a maximum 500 pounds")
            time.sleep(0.5)
        else:
            print("Your new balance is" , round(my_account.balance, 2))
    if int(my_account.balance + 500) < 0:
            print("You are going over your OVERDRAFT, WITHDRAW DENIED")
            my_account.deposit(withdrawAmmount)
main()
来自随机导入randint
导入时间
类别银行账户(对象):
定义初始(自身,初始余额=0):
self.balance=初始余额
def保证金(自身、金额):
自身余额+=金额
def提取(自身、金额):
自我平衡-=金额
def透支(自):
返回自平衡<0
余额=(兰迪特(100500))
我的账户=银行账户(余额)
我的账户余额
变量=0
打印(“欢迎使用您的银行帐户”)
时间。睡眠(1)
def main():
变量==0时:
时间。睡眠(2)
打印(“您当前的余额为”,我的账户余额为,英镑”)
睡眠时间(0.5)
打印(“您想存款还是取款”)
decision=输入(“写入“存款”或“提取”,区分大小写:”)
如果决策=“存款”:
存款金额=浮动(输入(“您想存多少:”)
我的账户存款(存款金额)
打印(“您的新余额为”,第二轮(我的账户余额,2))
elif决策==“撤回”:
Drawamount=float(输入(“您想提取多少:”)
我的账户。取款(取款金额)
打印(“您的新余额为”,第二轮(我的账户余额,2))
如果我的账户余额<0:
时间。睡眠(1)
打印(“透支金额不超过500英镑的您的账户”)
睡眠时间(0.5)
其他:
打印(“您的新余额为”,第二轮(我的账户余额,2))
如果int(我的账户余额+500)<0:
打印(“您正在透支,提款被拒绝”)
我的账户。存款(取款账户)
main()

要使用继承,必须定义另一个类,如下所示

class BankAccountWithInterest(BankAccount):
  def __init__(self, initial_balance=0, rate=0.1):
     BankAccount.__init__(self, initial_balance)
     self._rate = rate
  def interest(self):
     return self.balance * self._rate
BankAccountWithInterest
继承自类
BankAccount
。 必须调用父类的构造函数(传递
initial\u balance
参数)

在类
BankAccount
中,应该有一个
get\u balance
方法。派生类不应该访问self.balance,而应该调用它:

返回self.get_balance()*self.\u rate

继承就像有两个不同的类——一个名为“储蓄账户”,另一个名为“支票账户”——它们都来自一个基类“银行账户”。在您的示例中,我没有看到任何类型的真正继承。你想要完成的是什么?与你的问题无关,但是很好的OOP:透支值和验证应该在类BankAccount中,而不是在主程序中。你的代码如何说明你遇到的问题?它没有继承,只有类,
BankAccount