首页 > 其他 > 详细

1:简单工厂模式

时间:2015-10-08 00:20:58      阅读:210      评论:0      收藏:0      [点我收藏+]
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()

 

1:简单工厂模式

原文:http://www.cnblogs.com/pengsixiong/p/4859714.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!