Python逻辑电路

2024-06-16 15:45:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一个视频复制了这个程序,我认为AndGate类中的__init__函数是不必要的,因为AndGate类中没有要定义的新实例。有人能证实我的推理吗?在

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

Tags: selfnoneinputoutputreturnifinitdef
0条回答
网友
1楼 · 发布于 2024-06-16 15:45:39

您是正确的,AndGate类下的__init__不是必需的。(在python中用这个特定的示例和一个要验证的新类进行了测试)。它与如何处理python中的继承有关:父类的__init__函数被自动调用。在

相关问题 更多 >