# 逻辑门电路的类 练习类的继承
'''logicgate 逻辑门
binarygate 两个输入
andgate与门 orgate或门
与非门 或非门
异或门 同或门
unarygate 一个输入
notgate 非门'''
class LogicGate:
def __init__(self,n):
self.label = n
self.output = None
def getLael(self):
return self.label
def getOutput(self):
self.output = self.performGateLogic() #该方法并未定义,在创建子类的时候定义
return self.output
class BinaryGate(LogicGate):
def __init__(self,n):
super().__init__(n)
self.pinA = None
self.pinB = None
def getpinA(self):
if self.pinA : # 配合连接器时新增
return self.pinA.getFrom().getOutput()
return int(input("Enter pin A for gate" + self.getLael()+"-->"))
def getpinB(self):
if self.pinB : # 配合连接器时新增
return self.pinB.getFrom().getOutput()
return int(input("Enter pin B for gate" + self.getLael() + "-->"))
def setNextPin(self,source): # 该方法用于连结器连结两个逻辑门的时候
if self.pinA == None:
self.pinA = source
else:
if self.pinB == None:
self.pinB = source
else:
raise RuntimeError('ERROR:No empry pin')
class AndGate(BinaryGate):
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
'''重写基类的方法,实现逻辑门的输出'''
a = self.getpinA()
b = self.getpinB()
if a == 1 and b == 1:
return 1
return 0
class NotAndGate(BinaryGate):
'''与非门'''
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getpinA()
b = self.getpinB()
if a == 1 and b == 1:
return 0
return 1
class OrGate(BinaryGate):
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getpinA()
b = self.getpinB()
if a == 1 or b == 1:
return 1
return 0
class NotOrGate(BinaryGate):
'''或非门'''
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getpinA()
b = self.getpinB()
if a == 1 or b == 1:
return 0
return 1
class XORgate(BinaryGate):
'''异或门'''
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getpinA()
b = self.getpinB()
if a != b:
return 1
return 0
class XNORgate(BinaryGate):
'''同或门'''
def __init__(self,n):
super().__init__(n)
def performGateLogic(self):
a = self.getpinA()
b = self.getpinB()
if a == b:
return 1
return 0
class UnaryGate(LogicGate):
def __init__(self, n):
super().__init__(n)
self.pin = None
def getPin(self):
if self.pin:
return self.pin.getFrom().getOutput()
return int(input("Enter pin for gate" + self.getLael() + "-->"))
def setNextPin(self, source): # 该方法用于连结器连结两个逻辑门的时候
if self.pin == None:
self.pin = source
else:
raise RuntimeError('ERROR:No empry pin')
class NotGate(UnaryGate):
def __init__(self, n):
super().__init__(n)
def performGateLogic(self):
x = self.getPin()
if x == 1:
return 0
return 1
class Connector:
'''连接器类,用于连接两个逻辑门'''
def __init__(self, fgate, tgate):
self.fromGate = fgate
self.toGate = tgate
tgate.setNextPin(self) # 此处有疑问 此处的self为Connector类的实例
def getFrom(self):
return self.fromGate
def getTo(self):
return self.toGate
if __name__ == '__main__':
# g1 = AndGate('g1')
# print(g1.getOutput())
# n1 = NotGate('n1')
# print(n1.getOutput())
# o1 = OrGate('o1')
# print(o1.getOutput())
# g1 = AndGate('g1')
# g2 = AndGate('g2')
# g3 = OrGate('g3')
# g4 = NotGate('g4')
# c1 = Connector(g1,g3)
# c2 = Connector(g2,g3)
# c3 = Connector(g3,g4)
# print(g4.getOutput())
g5 = XORgate('g5')
print(g5.getOutput())
<<python数据结构与算法分析>>第一章:用类实现逻辑门电路
最新推荐文章于 2022-05-10 22:11:34 发布