码迷,mamicode.com
首页 > 其他好文 > 详细

1:简单工厂模式

时间:2015-10-08 00:21:19      阅读:180      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!