写一个老婆类, 实例化一个老婆,老婆有名字,身高,体重,并且还会吃饭.
class Food:
def __init__(self, name, weigh):
self.name = name
self.weigh = weigh
class Wife:
def __init__(self, name, high, weigh):
self.name = name
self.high = high
self.weigh = weigh
self.food = []
def eat(self, item):
self.food.append(item.name)
self.weigh += item.weigh
def __str__(self):
return f'你的老婆姓名为{self.name},身高是{self.high}cm,体重是{self.weigh}kg,吃了{self.food}'
food = Food('苹果', 0.15)
wife = Wife('士力架', 164, 46)
wife.eat(food)
print(wife)