Problems in Data Structures and Algorithms 中逻辑门代码中函数定义的解释
Explanation of a function definition in logic gate code in Problems in Data Structures and Algorithms
(我是 Python 和 OOP 的新手,所以如果我在任何时候使用了无意义的语言,请告诉我。)
本书 Problems in Data Structures and Algorithms 以分层方式实现二进制和一元逻辑门的代码。请在我的问题底部查看完整的相关代码。
我的特定查询是:在连接器 class 中,在构造函数中,有一行
tgate.setNextPin(self)
回到定义setNextPin函数的一元门:
def setNextPin(self,source):
if self.pin == None:
self.pin = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")
函数setNextPin中的"source"是函数setNextPin的输入。所以当我看到行 tgate.setNextPin(self) 时,它告诉我回到一元 class (tgate) 找到函数 setNextPin,并且输入 ("source") setNextPin 函数是 self,在本例中是 Connector class 的一个实例。但是,我真的不明白这是怎么回事。我看不出连接器 class 如何成为 setNextPin 中变量 "source" 的输入。
完整代码:
class LogicGate:
def __init__(self,n):
self.name = n
self.output = None
def getName(self):
return self.name
def getOutput(self):
self.output = self.performGateLogic()
return self.output
class UnaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pin = None
def getPin(self):
if self.pin == None:
return int(input("Enter Pin input for gate "+self.getName()+"-->"))
else:
return self.pin.getFrom().getOutput()
def setNextPin(self,source):
if self.pin == None:
self.pin = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")
class NotGate(UnaryGate):
def __init__(self,n):
UnaryGate.__init__(self,n)
def performGateLogic(self):
if self.getPin():
return 0
else:
return 1
class Connector:
def __init__(self, fgate, tgate):
self.fromgate = fgate
self.togate = tgate
tgate.setNextPin(self)
def getFrom(self):
return self.fromgate
def getTo(self):
return self.togate
Connector
class 表示用于将逻辑门相互连接的导线。 setNextPin()
被赋予 Connector
,并将其与 pin
属性 相关联。当它想要获得提供其输入的逻辑门时,它调用 self.pin.getFrom()
,该方法转到 Connector
getFrom()
方法,然后 returns Connector
的 fromgate
属性,这将是电线另一端的门。
首先我们要注意 setNextPin(self,source)
是 binaryGate
class 的方法,也是 unaryGate
.
在函数定义setNextPin(self,source)
中,self
是tgate(alias of a Gate class)
,source
是连接器class的实例。假设 aaa-->bbb
(a
连接到 b
)那么连接器将是对象 bbb 的 PIN 和连接器属性的别名。fromGate
将是先前 [=28= 的别名] 即 aaa
.
(我是 Python 和 OOP 的新手,所以如果我在任何时候使用了无意义的语言,请告诉我。)
本书 Problems in Data Structures and Algorithms 以分层方式实现二进制和一元逻辑门的代码。请在我的问题底部查看完整的相关代码。
我的特定查询是:在连接器 class 中,在构造函数中,有一行
tgate.setNextPin(self)
回到定义setNextPin函数的一元门:
def setNextPin(self,source):
if self.pin == None:
self.pin = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")
函数setNextPin中的"source"是函数setNextPin的输入。所以当我看到行 tgate.setNextPin(self) 时,它告诉我回到一元 class (tgate) 找到函数 setNextPin,并且输入 ("source") setNextPin 函数是 self,在本例中是 Connector class 的一个实例。但是,我真的不明白这是怎么回事。我看不出连接器 class 如何成为 setNextPin 中变量 "source" 的输入。
完整代码:
class LogicGate:
def __init__(self,n):
self.name = n
self.output = None
def getName(self):
return self.name
def getOutput(self):
self.output = self.performGateLogic()
return self.output
class UnaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pin = None
def getPin(self):
if self.pin == None:
return int(input("Enter Pin input for gate "+self.getName()+"-->"))
else:
return self.pin.getFrom().getOutput()
def setNextPin(self,source):
if self.pin == None:
self.pin = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")
class NotGate(UnaryGate):
def __init__(self,n):
UnaryGate.__init__(self,n)
def performGateLogic(self):
if self.getPin():
return 0
else:
return 1
class Connector:
def __init__(self, fgate, tgate):
self.fromgate = fgate
self.togate = tgate
tgate.setNextPin(self)
def getFrom(self):
return self.fromgate
def getTo(self):
return self.togate
Connector
class 表示用于将逻辑门相互连接的导线。 setNextPin()
被赋予 Connector
,并将其与 pin
属性 相关联。当它想要获得提供其输入的逻辑门时,它调用 self.pin.getFrom()
,该方法转到 Connector
getFrom()
方法,然后 returns Connector
的 fromgate
属性,这将是电线另一端的门。
首先我们要注意 setNextPin(self,source)
是 binaryGate
class 的方法,也是 unaryGate
.
在函数定义setNextPin(self,source)
中,self
是tgate(alias of a Gate class)
,source
是连接器class的实例。假设 aaa-->bbb
(a
连接到 b
)那么连接器将是对象 bbb 的 PIN 和连接器属性的别名。fromGate
将是先前 [=28= 的别名] 即 aaa
.