首页 > 学院 > 开发设计 > 正文

1:简单工厂模式

2019-11-14 16:58:30
字体:
来源:转载
供稿:网友
class Operate:    def __init__(self):        passclass 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.bclass Divide(Operate):    def result(self):        return self.a/self.bclass 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()

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表