用Python实现链式调用

用Python实现链式调用:

class person():
    def name(self,name):
        self.name = name
        return self

    def age(self,age):
        self.age = age
        return self

    def hobby(self,hobby):
        self.hobby=hobby
        return self

    def show(self):
        print("My name is",self.name,
              "and age is",self.age,"years old.",
              "My hobby is",self.hobby,"."
              )

a = person()
a.name("pipi").age(18).hobby("study").show()
posted @ 2020-07-02 14:47  Lance_王  阅读(388)  评论(0编辑  收藏  举报