2013-04-04 84 views
1

我正在做一些处理Python中的类的作业。但是我陷入了一个我不太明白的部分。python中的类的提取方法

它告诉我:

- >写了一个名为withdraw()方法提取从帐户中指定的指定金额。账户余额减少了方法参数中指定的金额。平衡只能降低如果参数小于规定的数额或等于平衡

这是我的计划

class Account: 
    def __init__(self,id=0): 
     self.__id = id 
     self.__balance = 0 
     self.__annualInterestRate = 0 

    def getid(self): 
     return self.__id 

    def getbalance(self): 
     return self.__balance 

    def getannualInterestRate(self): 
     return self.__getannualInterestRate 

    def setid(self): 
     self.__id = id 

    def setbalance(self): 
     self.__balance = balance 

    def getMonthlyInterestRate(self): 
     return self.__annualInterestRate/12 


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

那么我将不得不:

def withdraw(): 
    # I don't know what to do here 
+0

'getMonthlyInterestRate'和'getMonthlyInterest'缺少'self'参数 – 2013-04-04 09:17:33

+0

对于''getMonthlyInterestRate'and它getMonthlyInterest'说写命名getMonthlyInterest AA方法()返还每月的利息金额。每月利息金额可以使用余额*月利率计算。每月利率可以通过年利率除以12来计算。 – Jett 2013-04-04 09:22:24

+1

使用'self .__ balance * self.getMonthlyInterestRate()'而不是'self .__ balance * getMonthlyInterestRate()'。 – glglgl 2013-04-04 09:58:01

回答

2

代码中存在各种错误。我修改class A一个可执行的类包括withdraw功能

class Account: 
    def __init__(self,id=0): 
     self.__id = id 
     self.__balance = 0 
     self.__annualInterestRate = 0 

    def getid(self): 
     return self.__id 

    def getbalance(self): 
     return self.__balance 

    def getannualInterestRate(self): 
     return self.__annualInterestRate 

    def setid(self,id): 
     self.__id = id 

    def setbalance(self, balance): 
     self.__balance = balance 

    def setannualInterestRate(self, rate): 
     self.__annualInterestRate = rate 

    def getMonthlyInterestRate(self): 
     return self.__annualInterestRate/12 

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

    def withdraw(self, amount): 
     if amount <= self.__balance: 
      self.__balance -= amount 
      return True 
     else: 
      return False 
+0

谢谢,我刚开始学习有关课程。 :) – Jett 2013-04-04 09:29:27

3

您需要通过隐含的self参数(在这里和在几个更多的方法)和amount

def withdraw(self, amount): 
    # subtract amount from self.__balance 

在上课之前,您还应该阅读return陈述。