课本链接 Problem Solving with Algorithms and Data Structures
Chapter one
Inheritance
先看代码
@Problem Solving with Algorithms and Data Structures
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
我们首先创造了一个LogicGate的类,并定义了getLabel和getOutput两个方法。然而注意,在getOutput method 中有一个performGateLogic method. 在父类 LogicGate中我们并没有实现performGateLogic method. 因此参数self会自动指向实现了performGateLogic method 的子类。
@Problem Solving with Algorithms and Data Structures
class BinaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pinA = None
self.pinB = None
def getPinA(self):
return int(input("Enter Pin A input for gate "+ self.getLabel()+"-->"))
def getPinB(self):
return int(input("Enter Pin B input for gate "+ self.getLabel()+"-->"))
#-------------------------------------------------------
class UnaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pin = None
def getPin(self):
return int(input("Enter Pin input for gate "+ self.getLabel()+"-->"))
这里就是class BinaryGate 和class UnaryGate 继承了LogicGate 并call了parent class的init method, 有点类似于Java constructor中的super() method.
在这里class UnaryGate 和 class BinaryGate的constructor 也可以写成
def __init__(self,n):
super(BinaryGate, self)__init__(n).
这样就无需记住Superclass的名字。
@Problem Solving with Algorithms and Data Structures
class AndGate(BinaryGate):
def __init__(self,n):
BinaryGate.__init__(self,n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a==1 and b==1:
return 1
else:
return 0
Class AndGate 继承自BinaryGate,其具有performGateLogic method. 我们先创建一个AndGate的类。
@Problem Solving with Algorithms and Data Structures
>>> andgate = AndGate("G1")
>>> andgate.getOutput()
Enter Pin A input for gate G1-->1
Enter Pin B input for gate G1-->0
0
class andgate 先调用了来自于LogicGate中的getOutput method. 并找到了来自于class AndGate 中的performGateLogic 然后调用了父类BinaryGate的getPinA.
接下来我们需要在Logic Gate之间建立connection. 所以我们创建一个新的class 叫connector.
@Problem Solving with Algoritms and Data Structures
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
class connector 的两个输入参数是两个gate. 尤其要注意
tgate.setNextPin(self)
这一行代码。之后会具体解释。
由于要在两个Logic Gate之间建立联系,因此LogicGate的input不一定全部来自于外部,也有可能来自于其他LogicGate的输出。因此我们需要对class BinaryGate 和 UnaryGate进行修改。
@Problem Solving with Algoritms and Data Structures
class BinaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pinA = None
self.pinB = None
def getPinA(self):
if self.pinA == None:
return int(input("Enter Pin A input for gate "+self.getName()+"-->"))
else:
return self.pinA.getFrom().getOutput()
def getPinB(self):
if self.pinB == None:
return int(input("Enter Pin B input for gate "+self.getName()+"-->"))
else:
return self.pinB.getFrom().getOutput()
def setNextPin(self,source):
if self.pinA == None:
self.pinA = source
else:
if self.pinB == None:
self.pinB = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")
#---------------------------------------------------------
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")
主要是对getPin method 进行了修改,并增加了setNextPin method.
回到class connector中的这一句
tgate.setNextPin(self)
这其实是将tgate 的这个LogicGate class的pin
指向了一个connector的instance。也就意味着BinaryGate 或者UnaryGate class 中的self.pin
指向了一个connecto的实例,因此在getPin method 中return self.pin.getFrom().getOutput()
pin可以调用getFrom method.
全部的code
@Problem Solving with Algoritms and Data Structures
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 BinaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pinA = None
self.pinB = None
def getPinA(self):
if self.pinA == None:
return int(input("Enter Pin A input for gate "+self.getName()+"-->"))
else:
return self.pinA.getFrom().getOutput()
def getPinB(self):
if self.pinB == None:
return int(input("Enter Pin B input for gate "+self.getName()+"-->"))
else:
return self.pinB.getFrom().getOutput()
def setNextPin(self,source):
if self.pinA == None:
self.pinA = source
else:
if self.pinB == None:
self.pinB = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")
class AndGate(BinaryGate):
def __init__(self,n):
BinaryGate.__init__(self,n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a==1 and b==1:
return 1
else:
return 0
class OrGate(BinaryGate):
def __init__(self,n):
BinaryGate.__init__(self,n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a ==1 or b==1:
return 1
else:
return 0
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
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())