2014-07-14 56 views
0

好吧,我試圖遷移父類下面的一些類似的類,以便一次更改所有類。這是我的代碼:未綁定的方法__init __()必須使用CreditCard實例作爲第一個參數調用(改爲獲得VisaCreditCard實例)

class CreditCard(): 
    def __init__(self, name, short, tag, length): 
     self.name = name 
     self.short = short 
     self.tag = tag 
     self.length = length 
     self.CCnumber = tag 
     while (len(self.CCnumber) < self.length - 1): 
      rand_int = random.randrange(10) 
      rand_str = str(rand_int) 
      self.CCnumber = self.CCnumber + rand_str 
     if (length == 15): 
      d = CheckSumDigit(self.CCnumber) 
      self.CCnumber = self.CCnumber + d 
     if (not RigorousVerifyLuhn(self.CCnumber)): 
      ln = self.length - 1 
      clip = self.CCnumber[0:ln] 
      fulfilled = False 
      dig = 0 
      while (dig <= 9 and fulfilled == False): 
       cand = clip + str(dig) 
       if (RigorousVerifyLuhn(cand)): 
        fulfilled = True 
        self.CCnumber = cand 
       dig = dig + 1 
      if (fulfilled == False): 
       if (len(self.CCnumber) != self.length): 
        print("Invalid " + self.name + " number, LENGTH " + len(self.CCnumber) + " (" + self.CCnumber + ")") 
       else: 
        print("Invalid " + self.name + " number, LUHN " + "(" + self.CCnumber + ")") 



class AmexCreditCard(CreditCard): 

    def __init__(self): 
     self.NAME = 'American Express' 
     self.SHORT = 'AMEX' 
     self.TAG = '3' 
     self.LENGTH = 15 
     CreditCard.__init__(self, 'American Express', 'AMEX', '3', 15) 

class VisaCreditCard(): 

    def __init__(self): 
     self.NAME = 'Visa' 
     self.SHORT = 'VISA' 
     self.TAG = '4' 
     self.LENGTH = 16 
     CreditCard.__init__(self, self.NAME, self.SHORT, self.TAG, self.LENGTH) 

class MasterCardCreditCard(): 

    def __init__(self): 
     CreditCard.__init__(self, 'MasterCard', 'MC', '5', 16) 

class DiscoverCreditCard(): 

    def __init__(self): 
     CreditCard.__init__(self, 'Discover', 'DISC', '6011', 16) 

我有幾種不同的兒童信用卡語法風格,因爲我不確定它的正確語法。當我運行這個,我得到這個錯誤:

TypeError: unbound method __init__() must be called with CreditCard instance as first argument (got VisaCreditCard instance instead) 

任何人都可以幫助我解決這個錯誤?謝謝!

回答

4

你忘了從CreditCard繼承:

class VisaCreditCard(CreditCard): 

這同樣適用於你的MasterCardCreditCardDiscoverCreditCard類。

+0

只要注意到並修復它!謝謝! – user3835980

相關問題