【问题标题】:Python logic circuitPython逻辑电路
【发布时间】:2015-06-21 14:37:37
【问题描述】:

我从视频中复制了这个程序,我认为 AndGate 类中的__init__ 函数是不必要的,因为在 AndGate 类中没有要定义的新实例。有人可以证实我的推理吗?

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

    def getLabel(self):
        return self.label

    def getOutput(self):
        self.output = self.performGateLogic()
        return self.output


class BinaryGate(LogicGate):
    def __init__(self,n):
        LogicGate.__init__(self,n)
        self.pinA = None
        self.pinB = None
    def SetNextPin(self,source):
        if self.pinA == None:
            self.pinA = source #pin a became a instance of connector class, conntector.gate.pinA
        else:
            if self.pinB == None:
                self.pinB = source
            else:
               raise RuntimeError("Error: NO EMPTY PINS")

    def getA(self):
        if self.pinA == None:
            return int(input("Enter Pin A input for gate "+self.getLabel()+"-->"))
        else:
            return self.pinA.getfg().getOutput()

    def getB(self):
        if self.pinB == None:
            return int(input("Enter Pin B input for gate "+self.getLabel()+"-->"))
        else:
            return self.pinB.getfg().getOutput()


class AndGate(BinaryGate):
    def __init__(self,n):
        BinaryGate.__init__(self,n)

    def performGateLogic(self):
        a = self.getA()
        b = self.getB()

        if a == 1:
            if a == b:
                return 1
        else:
            return 0

【问题讨论】:

  • 你试试,看看有没有效果?

标签: python class oop initialization circuit


【解决方案1】:

你是对的,AndGate 类下的__init__ 是不必要的。 (用这个特定的例子和一个新的类在 python 中测试)。这与python中如何处理继承有关:自动调用父类的__init__函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    相关资源
    最近更新 更多