'''
#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)
'''
Python简化类例:实现链式写法
最新推荐文章于 2024-02-07 20:08:44 发布