class result: def __init__(self, numA, numB): self.numA = numA self.numB = numB def getResult(self): print('计算两个操作数的运算结果。。') class OpearAdd(result): def __init__(self,numA,numB): super(OpearAdd,self).__init__(numA,numB) def getResult(self): return self.numA+self.numB class OpearJian(result): def __init__(self,numA,numB): super(OpearJian,self).__init__(numA,numB) def getResult(self): return self.numA-self.numB class OpearChu(result): def __init__(self,numA,numB): super(OpearChu,self).__init__(numA,numB) def getResult(self): return self.numA/self.numB class OpearChen(result): def __init__(self,numA,numB): super(OpearChen,self).__init__(numA,numB) def getResult(self): return self.numA*self.numB class pry(object): #根据参数不同创建不同对象 def creat_class(self,fu,numA,numB): if fu == '+': q = OpearAdd(numA, numB); elif fu == '-': q = OpearJian(numA, numB); elif fu == '*': q = OpearChen(numA, numB); elif fu == '/': q = OpearChu(numA, numB) else: print('输入错误!') return q; if __name__=='__main__': #工厂对象 factoy=pry(); numA=int(input('请输入第一个数:')) fu=input('请输入运算符:') numB=int(input('请输入第二个数:')) #q 代表由工厂创建出来的一个计算器 q=factoy.creat_class(fu,numA,numB) # q=None; # if fu=='+': # q=OpearAdd(numA,numB); # elif fu=='-': # q=OpearJian(numA,numB); # elif fu=='*': # q=OpearChen(numA,numB); # elif fu=='/': # q=OpearChu(numA,numB) # else: # print('输入错误!') print('计算结果:',q.getResult())
计算机案例
最新推荐文章于 2021-07-24 01:40:39 发布