1:简单工厂模式

class Operate:
    def __init__(self):
        pass


class Add(Operate):

    def result(self):
        return (self.a+self.b)

class Subduct(Operate):

    def result(self):
        return self.a-self.b
        
class Multiply(Operate):

    def result(self):
        return self.a*self.b

class Divide(Operate):

    def result(self):
        return self.a/self.b

class Factory:                            #简单工厂,实现各个类实例化交互的过程

    def run(self):
        op = Operate                       #注意:a,b,operation都是运算父类Operate的 ‘类属性’ 这样其子类才能够直接调用,子类不能访问父类的实例属性
        op.a = int(input('plz input the first number:'))
        op.operation = input('plz input operation like(+,-,*,/):')
        op.b = int(input('plz input the second number:'))
        
        if(op.operation == '+'):
            print(Add().result())
        elif(op.operation == '-'):
            print(Subduct().result())
        elif(op.operation == '*'):
            print(Multiply().result())
        elif(operation == '/'):
            print(Divide().result())


Factory().run()

 

posted @ 2015-10-07 22:39  billiepander  阅读(384)  评论(0编辑  收藏  举报