Python 类及其继承

  • 深刻理解python面向对象的意义,创建类、并使用类继承。

1、实现分数的相关运算

class Fraction:
    def __init__(self, top, bottom):
        self.num = top # 分子
        self.den = bottom  # 分母
    
    def show(self):
        print(self.num, "/", self.den)
        
    def __str__(self):
        common = self.gcd(self.num, self.den)
        return str(self.num//common) + "/" + str(self.den//common)
    
    def gcd(self, m, n):
        '''求两个数的最大公约数,用于分数的化简'''
        while m%n != 0:
            oldm = m
            oldn = n
            m = oldn
            n = oldm%oldn
        return n       
    
    def __add__(self, otherFraction):
        newnum = self.num * otherFraction.den + self.den * otherFraction.num
        newden = self.den * otherFraction.den
        common = self.gcd(newnum, newden)
        return Fraction(newnum//common,newden//common)
    
    def __eq__(self,other):
        firstnum = self.num * other.den
        secondnum = self.den * other.num
        return firstnum == secondnum
    
    def __sub__(self, other):
        newnum = self.num * other.den -self.den * other.num
        newden = self.den * other.den
        common = self.gcd(newnum, newden)
        return Fraction(newnum//common , newden//common)
    
    def __mul__(self, other):
        newnum = self.num * other.num
        newden = self.den * other.den
        common = self.gcd(newnum, newden)
        return Fraction(newnum//common , newden//common)
    
    def __truediv__(self, other):
        # 注意:python3除法的名不再是__div__,而是 __truediv__
        newnum = self.num * other.den
        newden = self.den * other.num
        common = self.gcd(newnum, newden)
        return Fraction(newnum//common , newden//common)
    
    def __lt__(self, other):
        '''小于'''
        newnum = self.num * other.den -self.den * other.num
        return newnum < 0
    
    def __gt__(self, other):
        '''大于'''
        newnum = self.num * other.den -self.den * other.num
        return newnum > 0
    
f1 = Fraction(1,4)
f2 = Fraction(1,2)
f3 = Fraction(3,5)
f4 = Fraction(4,8)
print(f1 + f2)
3/4
print(f1 - f2)
-1/4
print(f1 + f3)
17/20
f1 == f3
False
f2 == f4
True
print(f2 - f4)
0/1
f1 < f2
True
f1 > f2
False
print(f1 * f2, f2 * f3, f2 * f4 )
1/8 3/10 1/4
print(f1 / f2, f2 / f3, f2 / f4)
1/2 5/6 1/1

2、逻辑电路仿真

  • 实现了与、或、非门得电路设计
class LogicGate:
    def __init__(self, name):
        self.label = name
        self.output = None
        
    def getLabel(self):
        return self.label
    
    def getOutput(self):
        self.output = self.performGateLogic()
        return self.output
  • 上述代码并没有实现performGateLogic()函数,是因为想让后面具体的逻辑电路实现。
class BinaryGate(LogicGate):
    def __init__(self,name):
        LogicGate.__init__(self,name) # 相当于 super(BinaryGate,self).__init__(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() + "-->"))
    
    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,name):
        LogicGate.__init__(self,name)
        self.pin = None

    def getPin(self):
        if self.pin == None:
            return int(input("Enter Pin input for gate "+self.getLabel()+"-->"))
        else:
            return self.pin.getFrom().getOutput()
    
    def setNextPin(self, source):
        if self.pin == None:
            self.pin = source
        else:
            raise RuntimeError("Error: No Empty Pins")

class AndGate(BinaryGate):
    def __init__(self, name):
        BinaryGate.__init__(self, name)
        
    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, name):
        BinaryGate.__init__(self, name)
        
    def performGateLogic(self):
        a = self.getPinA()
        b = self.getPinB()
        if a==0 and b==0:
            return 0
        else:
            return 1
class NotGate(UnaryGate):
    def __init__(self, name):
        UnaryGate.__init__(self, name)
        
    def performGateLogic(self):
#         a = self.getPin()
#         return 1 if a==0 else 0

        if self.getPin():
            return 0
        else:
            return 1
'''测试'''
g1 = AndGate("G1")
g1.getOutput()
Enter Pin A input for gate G1-->1
Enter Pin B input for gate G1-->1





1
g2 = OrGate("G2")
g2.getOutput()
Enter Pin A input for gate G2-->1
Enter Pin B input for gate G2-->0





1
g3 = NotGate("G3")
g3.getOutput()
Enter Pin input for gate G3-->1





0
  • “电子元件”设置好之后,便可以连接电路了
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
    
    
    
    def getPinA(self):
        if self.pinA ==None:
            return input("Enter Pin A input for gate " + self.getName()+"-->")
        else:
            return self.pinA.getFrom().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)
g4.getOutput()
Enter Pin A input for gate G3-->1
Enter Pin B input for gate G3-->0





0
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值