用python编程,定义一个学生类,创建两个学生对象,实现二人关于学习体能唱歌的对话。
时间: 2024-03-15 10:47:02 浏览: 62
好的,以下是Python代码实现:
```python
class Student:
def __init__(self, name):
self.name = name
def learn(self):
print(f"{self.name} is studying hard.")
def exercise(self):
print(f"{self.name} is exercising to keep fit.")
def sing(self):
print(f"{self.name} is singing a song.")
# 创建两个学生对象
student1 = Student("Tom")
student2 = Student("Jerry")
# 实现二人关于学习,体能,唱歌的对话
student1.learn()
student2.learn()
student1.exercise()
student2.exercise()
student1.sing()
student2.sing()
```
输出结果:
```
Tom is studying hard.
Jerry is studying hard.
Tom is exercising to keep fit.
Jerry is exercising to keep fit.
Tom is singing a song.
Jerry is singing a song.
```
阅读全文