Python简化类例:实现链式写法

在这里插入图片描述

'''


#Python简化类例五:实现链式写法
def ClassTest5():
    '''链式写法只要是类语言都可以实写,个人比较喜欢,也就顺手尝试一下'''
    def p() :pass
    p.x=0
    p.y=0
    def o():pass
 
    def printPos():
        print("当前坐标是(%d, %d)"%(p.x,p.y))
 
    def moveLeft():
        p.x-=1
        printPos()
        return o
    o.moveLeft=moveLeft;del moveLeft
 
    def moveTop():
        p.y+=1
        printPos()
        return o
    o.moveTop=moveTop;del moveTop
 
    def moveRight():
        p.x+=1
        printPos()
        return o
    o.moveRight=moveRight;del moveRight
 
    def moveBottom():
        p.y-=1
        printPos()
        return o
    o.moveBottom=moveBottom;del moveBottom
    return o
 
t5=ClassTest5()
t5.moveTop().moveLeft().moveBottom().moveRight()
'''
打印结果:
当前坐标是(0, 1)
当前坐标是(-1, 1)
当前坐标是(-1, 0)
当前坐标是(0, 0)
'''

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45342712/article/details/95193460