2015-04-15 72 views
0

我從視頻中複製這個程序,我認爲AndGate類中的__init__函數是不必要的,因爲AndGate類中沒有新的實例要定義。有人可以證實我的推理嗎?Python邏輯電路

class LogicGate: 
    def __init__(self,n): 
     self.label = n 
     self.output = None 

    def getLabel(self): 
     return self.label 

    def getOutput(self): 
     self.output = self.performGateLogic() 
     return self.output 


class BinaryGate(LogicGate): 
    def __init__(self,n): 
     LogicGate.__init__(self,n) 
     self.pinA = None 
     self.pinB = None 
    def SetNextPin(self,source): 
     if self.pinA == None: 
      self.pinA = source #pin a became a instance of connector class, conntector.gate.pinA 
     else: 
      if self.pinB == None: 
       self.pinB = source 
      else: 
       raise RuntimeError("Error: NO EMPTY PINS") 

    def getA(self): 
     if self.pinA == None: 
      return int(input("Enter Pin A input for gate "+self.getLabel()+"-->")) 
     else: 
      return self.pinA.getfg().getOutput() 

    def getB(self): 
     if self.pinB == None: 
      return int(input("Enter Pin B input for gate "+self.getLabel()+"-->")) 
     else: 
      return self.pinB.getfg().getOutput() 


class AndGate(BinaryGate): 
    def __init__(self,n): 
     BinaryGate.__init__(self,n) 

    def performGateLogic(self): 
     a = self.getA() 
     b = self.getB() 

     if a == 1: 
      if a == b: 
       return 1 
     else: 
      return 0 
+3

你如何嘗試,看看它是否有效? – deets

回答

1

你是正確的,__init__AndGate類下是沒有必要的。 (用python測試這個具體的例子和一個新的類來驗證)。它與如何處理python中的繼承有關:自動調用父類的__init__函數。