class Car(object):
def __init__(self, newWwheelNum,newColor):
self.wheel = newWwheelNum
self.color = newColor
def __str__(self):
msg = "嘿,我的颜色是:"+self.color+"我有"+str(self.wheel)+"个轮子》》》"
return msg
def move(self):
print('车在跑,目标是夏威夷')
b = Car(4,"白色")
print(b)
总结
1、在python中方法名如果是__xxxx__()的,那么就有特殊的功能,因此叫做“魔法”方法
2、当使用print输出对象的时候,只要自己定义了__str__(self)方法,那么就会打印从在这个方法中return的数据