鱼C论坛

 找回密码
 立即注册
查看: 1040|回复: 1

两个逻辑门的语法问题

[复制链接]
发表于 2020-3-8 23:30:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. class LogicGate:

  2.     def __init__(self,n):
  3.         self.name = n
  4.         self.output = None

  5.     def getName(self):
  6.         return self.name

  7.     def getOutput(self):
  8.         self.output = self.performGateLogic()
  9.         return self.output


  10. class BinaryGate(LogicGate):

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

  13.         self.pinA = None
  14.         self.pinB = None

  15.     def getPinA(self):
  16.         if self.pinA == None:
  17.             return int(input("Enter Pin A input for gate "+self.getName()+"-->"))
  18.         else:
  19.             return self.pinA.getFrom().getOutput()

  20.     def getPinB(self):
  21.         if self.pinB == None:
  22.             return int(input("Enter Pin B input for gate "+self.getName()+"-->"))
  23.         else:
  24.             return self.pinB.getFrom().getOutput()

  25.     def setNextPin(self,source):

  26.         if self.pinA == None:
  27.             self.pinA = source

  28.         else:
  29.             if self.pinB == None:
  30.                 self.pinB = source
  31.             else:
  32.                 print("Cannot Connect: NO EMPTY PINS on this gate")


  33. class AndGate(BinaryGate):

  34.     def __init__(self,n):
  35.         BinaryGate.__init__(self,n)

  36.     def performGateLogic(self):

  37.         a = self.getPinA()
  38.         b = self.getPinB()
  39.         if a==1 and b==1:
  40.             return 1
  41.         else:
  42.             return 0

  43. class OrGate(BinaryGate):

  44.     def __init__(self,n):
  45.         BinaryGate.__init__(self,n)

  46.     def performGateLogic(self):

  47.         a = self.getPinA()
  48.         b = self.getPinB()
  49.         if a ==1 or b==1:
  50.             return 1
  51.         else:
  52.             return 0

  53. class UnaryGate(LogicGate):

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

  56.         self.pin = None

  57.     def getPin(self):
  58.         if self.pin == None:
  59.             return int(input("Enter Pin input for gate "+self.getName()+"-->"))
  60.         else:
  61.             return self.pin.getFrom().getOutput()

  62.     def setNextPin(self,source):
  63.         if self.pin == None:
  64.             self.pin = source
  65.         else:
  66.             print("Cannot Connect: NO EMPTY PINS on this gate")


  67. class NotGate(UnaryGate):

  68.     def __init__(self,n):
  69.         UnaryGate.__init__(self,n)

  70.     def performGateLogic(self):
  71.         if self.getPin():
  72.             return 0
  73.         else:
  74.             return 1


  75. class Connector:

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

  79.         tgate.setNextPin(self)


  80.     def getFrom(self):
  81.         return self.fromgate

  82.     def getTo(self):
  83.         return self.togate


  84. def main():
  85.    g1 = AndGate("G1")
  86.    g2 = AndGate("G2")
  87.    g3 = OrGate("G3")
  88.    g4 = NotGate("G4")
  89.    c1 = Connector(g1,g3)
  90.    c2 = Connector(g2,g3)
  91.    c3 = Connector(g3,g4)
  92.    print(g4.getOutput())

  93. main()
复制代码

def setNextPin(self,source)调用的时候source的参数为self,这个self是指什么,不是c1,不是fgate,不是tgate,我都试了,
再就是return self.pinB.getFrom().getOutput(),为什么不是直接self.pinB().getOutput()

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-9 10:22:17 | 显示全部楼层
自己研究了一下,传入source的确实是一个实例对象c1或者c2或者c3,那self.pinA = source又是什么含义呢,请大佬们指点
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-2 10:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表