(1) 创建父类Person类,包括name,age成员变量;setter和getter方法;定义printPerson打印人的信息 (2) 创建子类Student继承Person父类,新增className成员变量,变量的封装setter和getter,定义printStudent方法打印学生信息 (3) 在子类中实现对父类的printPerson()的重写 (4) 创建Person和Student对象,Person对象调用printPerson()方法,Student对象调用printPerson()方法和printStudent()方法
时间: 2023-12-29 16:02:37 浏览: 80
好的,这是一个关于面向对象编程的问题,下面是代码实现:
```python
class Person:
def __init__(self, name, age):
self.__name = name
self.__age = age
def set_name(self, name):
self.__name = name
def set_age(self, age):
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def printPerson(self):
print("Name:", self.__name)
print("Age:", self.__age)
class Student(Person):
def __init__(self, name, age, className):
super().__init__(name, age)
self.__className = className
def set_className(self, className):
self.__className = className
def get_className(self):
return self.__className
def printStudent(self):
super().printPerson()
print("Class Name:", self.__className)
def printPerson(self):
print("Student's Information:")
self.printStudent()
person = Person("John", 25)
person.printPerson()
student = Student("Alice", 19, "Mathematics")
student.printPerson()
student.printStudent()
```
输出结果为:
```
Name: John
Age: 25
Student's Information:
Name: Alice
Age: 19
Class Name: Mathematics
```
阅读全文