码迷,mamicode.com
首页 > 编程语言 > 详细

python面向对象编程(很重要)

时间:2018-09-17 13:32:22      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:log   and   family   lse   enter   编程   else   cga   not   


class LogicGate():
def __init__(self,n):
self.name=n
def getname(self):
return self.name
def getoutput(self):
output=self.out()
return output
class BinaryGate(LogicGate):
def __init__(self,n):
super(BinaryGate,self).__init__(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().out()

def getpinB(self):
if self.pinB==None:
return int(input(‘Enter pin B input for gate ‘+self.getname()+‘-->‘))
else:
return self.pinB.getfrom().out() #pinB不能去掉,如果去掉BinaryGate类没有getfrom方法,还是有点疑问

def setnextpin(self, source):
if self.pinA == None:
self.pinA = source
elif self.pinB == None:
self.pinB = source
else:
print(‘Cannot Connect: NO EMPTY PINS on this gate‘)
class Unary(LogicGate):
def __init__(self,n):
super(Unary, self).__init__(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().out()

def setnextpin(self, source):
if self.pin == None:
self.pin = source
else:
print(‘Cannot Connect: NO EMPTY PINS on this gate‘)


class andgate(BinaryGate):
def __init__(self,n):
super(andgate,self).__init__(n)
def out(self):
self.pinA=self.getpinA()
while (self.pinA != 0)and(self.pinA != 1):
self.pinA = self.getpinA()
self.pinB=self.getpinB()
while (self.pinB != 0)and(self.pinB != 1):
self.pinB = self.getpinB()
return self.pinA & self.pinB


class OrGate(BinaryGate):
def __init__(self,n):
super(OrGate, self).__init__(n)
def out(self):
self.pinA=self.getpinA()
while (self.pinA != 0)and(self.pinA != 1):
self.pinA = self.getpinA()
self.pinB=self.getpinB()
while (self.pinB != 0)and(self.pinB != 1):
self.pinB = self.getpinB()
return self.pinA | self.pinB
class NotGate(Unary):
def __init__(self,n):
super(NotGate, self).__init__(n)
self.pin=None
def out(self):
self.pin = self.getpin()
while (self.pin != 0) and (self.pin != 1):
self.pin = self.getpin()
if self.pin==1:
result=0
elif self.pin==0:
result=1
else:
print(‘input is an error!‘)
return result


class Connector():
def __init__(self,fromgate,togate):
self.fromgate=fromgate
self.togate=togate
self.togate.setnextpin(self) #连接器往下流选择pinA还是pinB
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.out())

python面向对象编程(很重要)

标签:log   and   family   lse   enter   编程   else   cga   not   

原文地址:https://www.cnblogs.com/masterhu/p/9661488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!