定义一个Person类,该类包括age,name两个数据成员和eat(),work()两个成员方法,并实现对两个数据成员的Getter方法。然后通过测试程序,实现相应对象的操作。程序输出结果如下:
时间: 2024-04-29 09:25:32 浏览: 128
C++_类和对象
```python
class Person:
def __init__(self, age, name):
self.age = age
self.name = name
def eat(self):
print("{} is eating".format(self.name))
def work(self):
print("{} is working".format(self.name))
def get_age(self):
return self.age
def get_name(self):
return self.name
person1 = Person(30, "Tom")
person2 = Person(25, "Mary")
print("{}'s age is {}".format(person1.get_name(), person1.get_age()))
person1.eat()
person2.work()
```
输出结果:
```
Tom's age is 30
Tom is eating
Mary is working
```
阅读全文