定义一个学生类,创建两个学生对象,实现二人对话。
时间: 2024-03-15 13:47:40 浏览: 52
java定义一个学生类
好的,以下是实现代码:
```python
class Student:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def introduce(self):
print("My name is %s, I am %d years old, and I am %s." % (self.name, self.age, self.gender))
# 创建两个学生对象
student1 = Student("Tom", 18, "male")
student2 = Student("Lucy", 17, "female")
# 二人对话
print("Student1: ")
student1.introduce()
print("Student2: ")
student2.introduce()
```
输出结果如下:
```
Student1:
My name is Tom, I am 18 years old, and I am male.
Student2:
My name is Lucy, I am 17 years old, and I am female.
```
这样就实现了两个学生对象之间的对话。
阅读全文