定义一个学生类,创建两个学生对象,实现二人对话
时间: 2024-03-15 08:46:55 浏览: 49
好的,下面是一个简单的学生类定义和两个学生对象的对话实现:
```python
class Student:
def __init__(self, name):
self.name = name
def talk(self, other):
print(f"{self.name}: 你好,{other.name}!")
print(f"{other.name}: 你好,{self.name}!")
# 创建两个学生对象
s1 = Student("小明")
s2 = Student("小红")
# 二人对话
s1.talk(s2)
```
输出结果如下:
```
小明: 你好,小红!
小红: 你好,小明!
```
相关问题
定义一个学生类,创建两个学生对象,实现二人对话。
好的,以下是实现代码:
```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.
```
这样就实现了两个学生对象之间的对话。
用python编程,定义一个学生类,创建两个学生对象,实现二人对话。
好的,以下是Python代码实现:
```python
class Student:
def __init__(self, name):
self.name = name
def say_hello(self):
print(f"Hello, my name is {self.name}.")
# 创建两个学生对象
student1 = Student("Tom")
student2 = Student("Jerry")
# 实现二人对话
student1.say_hello()
student2.say_hello()
```
输出结果:
```
Hello, my name is Tom.
Hello, my name is Jerry.
```
阅读全文