chaining_method.py 629 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. class Person(object):
  4. def __init__(self, name, action):
  5. self.name = name
  6. self.action = action
  7. def do_action(self):
  8. print self.name, self.action.name,
  9. return self.action
  10. class Action(object):
  11. def __init__(self, name):
  12. self.name = name
  13. def amount(self, val):
  14. print val,
  15. return self
  16. def stop(self):
  17. print 'then stop'
  18. if __name__ == '__main__':
  19. move = Action('move')
  20. person = Person('Jack', move)
  21. person.do_action().amount('5m').stop()
  22. #===== Output =====
  23. # Jack move 5m then stop