问答详情
源自:2-8 Python定义实例方法

想请教一下参考答案中定义修改set方法,要怎么调用呢

想请教一下参考答案中定义修改set方法,要怎么调用呢

提问者:有目标的蜗牛 2022-01-07 21:14

个回答

  • weixin_慕妹7068926
    2022-05-17 17:08:11

    使用get

  • weixin_慕设计1349666
    2022-01-08 21:21:09

    class Animal():

        def __init__(self,name,age,location):

            self.__name=name

            self.__age=age

            self.__location=location

        def get_info(self):

            return 'name={},\nage={},\nlocation={}'.format(self.__name,self.__age,self.__location)

        def set_name(self,name):

            self.__name=name

        def set_age(self,age):

            self.__age=age

        def set_location(self,location):

            self.__location=location

            

    dog=Animal("hashiqi",4,"guangzhou")

    print(dog.get_info())

    print("")

    dog.set_location("tianjin")

    print(dog.get_info())