用python写一个Person函数类,有姓名、年龄、身高、体重等属性,有吃饭等方法
时间: 2024-05-24 09:12:23 浏览: 164
基于python3 类的属性、方法、封装、继承实例讲解
5星 · 资源好评率100%
class Person:
def __init__(self, name, age, height, weight):
self.name = name
self.age = age
self.height = height
self.weight = weight
def eat(self):
print("{} is eating".format(self.name))
def sleep(self):
print("{} is sleeping".format(self.name))
def __str__(self):
return "{} is {} years old, {}m tall and {}kg".format(self.name, self.age, self.height, self.weight)
# Example usage
person1 = Person("John", 25, 1.75, 70)
print(person1)
person1.eat()
person1.sleep()
阅读全文