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

时间:2014-12-21 17:32:59

标签: python class python-3.x

我有一个LogicGate类,它可以有一个或两个输入行(分别是UnaryGateBinaryGate个子类)和一个Connector类,其中包含两个{ {1}}对象并且具有将一个门的输出返回到另一个门的方法,因此接收端的门可以对其执行操作。我理解的是在这里实际执行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 self.pin.get_from().get_output()方法中做了什么?如果get_pin()效果UnaryGateget_pin()' Connector,那么togate会返回相应的get_from(),而fromgate会执行get_output() 1并根据从其连接的一个或两个门接收的输入返回0self.pin.get_from()。我无法绕过的是get_from()部分。在pin对象上调用了1,根据我的理解,这只是一个类属性,可以是0None或{{1}}?这对我来说没有多大意义,因为我不知道它如何能够执行该方法。数据的确切转移方式是什么?在一个门和它连接到的门之间?

1 个答案:

答案 0 :(得分:0)

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

>>> 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
>>> 

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

相关问题