定义一个学生类,创建两个学生对象,实现二人对话
时间: 2024-03-15 18:47:27 浏览: 12
好的,以下是 Python 代码实现:
```python
class Student:
def __init__(self, name):
self.name = name
def talk(self, message):
print(f"{self.name}: {message}")
# 创建两个学生对象
s1 = Student("Tom")
s2 = Student("Lucy")
# 实现二人对话
s1.talk("Hi, Lucy!")
s2.talk("Hi, Tom! How are you?")
s1.talk("I'm good. How about you?")
s2.talk("I'm fine too. What are you doing now?")
s1.talk("I'm preparing for the final exams. How about you?")
s2.talk("Me too. Let's study together sometime.")
s1.talk("Sure, that sounds like a good idea!")
```
输出结果:
```
Tom: Hi, Lucy!
Lucy: Hi, Tom! How are you?
Tom: I'm good. How about you?
Lucy: I'm fine too. What are you doing now?
Tom: I'm preparing for the final exams. How about you?
Lucy: Me too. Let's study together sometime.
Tom: Sure, that sounds like a good idea!
```