class Person: def __init__(self,name,weight): self.name = name self.weight = weight def __str__(self): return '我叫%s,我的体重是%.2f' % (self.name, self.weight) # %f,保留小数,2f指保留两位小数 def run(self): self.weight -= 0.5 def eat(self): self.weight += 1 xiaoming = Person('小明',75) xiaoming.run() xiaoming.eat() print(xiaoming)
来源:https://www.cnblogs.com/yeyu1314/p/12550306.html