繁体   English   中英

逻辑门类-执行流程在这里到底如何工作?

[英]Logic gate classes - how exactly does the flow of execution work here?

我有一个LogicGate类,可以有一个或两个输入行(分别是UnaryGateBinaryGate子类),还有一个Connector类,它包含两个LogicGate对象,并且有一个将一个门的输出返回到另一个门的方法,因此接收端的门可以对其执行操作。 我无法理解的是什么对象实际上在这里执行get_from()方法:

class LogicGate:

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

    def get_label(self):
        return self.label

    def get_output(self):
        self.output = self.perform_gate_logic()
        return self.output

class UnaryGate(LogicGate):

    def __init__(self,n):
        LogicGate.__init__(self,n)

        self.pin = None

    def get_pin(self):
        if self.pin == None:
            pin = int(input("Enter Pin input for gate " + self.get_label() + "-->"))
            while pin != 1 and pin != 0:
                pin = int(input("Try again with 1 or 0 -->"))
            return pin
        else:
            return self.pin.get_from().get_output()

    def set_next_pin(self,source):
        if self.pin == None:
            self.pin = source
        else:
            raise RuntimeError("ERROR: NO EMPTY PINS")

class Connector:

    def __init__(self,fgate,tgate):
        self.fromgate = fgate
        self.togate = tgate

        tgate.set_next_pin(self)

    def get_from(self):
        return self.fromgate

    def get_to(self):
        return self.togate

get_pin()方法中self.pin.get_from().get_output()到底做什么? 如果执行get_pin()UnaryGateConnectortogate ,则get_from()返回相应的fromgate ,后者依次执行get_output()并根据从其连接的一两个门接收到的输入返回10 。 。 我无法确定的是self.pin.get_from()部分。 get_from()被调用的上pin对象,我的理解就是一个类的属性,可以是10None 这对我来说意义不大,因为我不知道它如何执行该方法。 数据在门和它所连接的门之间如何“传输”?

当闸门尚未收到用户的任何输入时,引脚实际上是连接器,如以下解释器会话所示:

>>> g1 = AndGate("g1")
>>> g2 = AndGate("g2")
>>> g3 = OrGate("g3")
>>> g4 = NotGate("g4")
>>> c1 = Connector(g1,g3)
>>> c2 = Connector(g2,g3)
>>> c3 = Connector(g3,g4)
>>> repr(g4)
'<__main__.NotGate object at 0x102958890>'
>>> g4.pin
<__main__.Connector object at 0x10297f450>
>>> g4.pin == c3
True
>>> g3.pin_a
<__main__.Connector object at 0x10297f590>
>>> g3.pin_a == c1
True
>>> g3.pin_b == c2
True
>>> g1.pin_a
>>> print(g1.pin_a)
None
>>> 

现在,我了解了他们如何执行该方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM