class Cat: def __init__(self,_name): self.name = _name def __str__(self): return "i am %s"%self.name def show(self): print("name is %s"%self.name) tom = Cat("tom") tom.show() print(tom) print("------------"); lanmao = Cat("lanpang") lanmao.show() print(lanmao)
__str__函数要求有返回值,必须有return, __str__函数功能是将对象字符串化
原文地址:https://www.cnblogs.com/zhanggaofeng/p/9534122.html
时间: 2024-11-16 10:05:22