面向对象(__str__和__repr__方法)
1 #Author : Kelvin 2 #Date : 2019/1/21 16:19 3 class App: 4 def __init__(self,name): 5 self.name=name 6 # def __str__(self): 7 # return "名字是%s"%self.name 8 # def __repr__(self): 9 # return "repr方法!" 10 a=App("kelvin") 11 print(a) 12 13 """ 14 如果类中没有str和repr方法则打印:<__main__.App object at 0x00000266BDFD2F60> 15 如果类中同时存在str和repr方法则打印str方法的返回值 16 如果类中存在其一,则打印该方法的返回值。 17 """
本质:自定制打印内容。
❤️ 如果你喜欢这篇文章,请点赞支持! 👍 同时欢迎关注我的博客,获取更多精彩内容!
本文来自博客园,作者:佛祖让我来巡山,转载请注明原文链接:https://www.cnblogs.com/sun-10387834/p/10299455.html

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号