要求在Student类和Teacher类中分别实现printInfo方法,该方法打印对象的多有属性信息。
时间: 2024-02-18 17:59:25 浏览: 69
VS C++2005类与对象的课件
好的,我明白了。下面是可以参考的Student类和Teacher类的代码:
```
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
def printInfo(self):
print("Name: ", self.name)
print("Age: ", self.age)
print("Grade: ", self.grade)
class Teacher:
def __init__(self, name, age, subject):
self.name = name
self.age = age
self.subject = subject
def printInfo(self):
print("Name: ", self.name)
print("Age: ", self.age)
print("Subject: ", self.subject)
```
上述代码中,Student类和Teacher类都实现了printInfo方法,该方法会打印对象的所有属性信息。当你创建一个Student或Teacher对象,然后调用它的printInfo方法,该对象的所有属性信息就会被打印出来。
阅读全文