'''
重写:将函数重新定义写一遍
__str__():再调用print 打印对象时自动调用,是给用户用的是一个描述对象的方法
__repr__():是给机器用的,在python解释器里面直接敲对象名再回车调用的方法
注意:在没有str时,且有repr,str = repr
'''
class Person(object):
def __init__(self,name,age,height,weight):
self.name = name
self.height = height
self.age =age
self.weight = weight
def __repr__(self):
return "这里是" "%s-%d-%d-%d"%(self.name,self.age,self.height,self.weight)
per1 = Person("hanmeimei ",21,160,80)
# print(per1.name,per1.age,per1.weight)
print(per1)
#优点:当一个对象的属性值很多,并且都需要打印,重写了__str__函数,简化代码
#作业:人开枪射击子弹
重写__repr__与__str__函数
猜你喜欢
转载自blog.csdn.net/qq_41856814/article/details/89367217
今日推荐
周排行