python 定义一个Person类有name, age, sex的公有成员变量,以及一个showinfor()的方法对该类的三个属性进行打印。再定义一个Teacher类,该类继承Person类,并且额外新增一个department的公有成员变量,也对Teacher类定义一个showinfor()的方法对该类的四个属性进行打印。再定义一个Student类,该类继承Person类,并且额外新增一个marks的公有成员变量,也对Student类定义一个showinfor()的方法对该类的四个属性进行打印。
时间: 2023-06-05 22:06:04 浏览: 91
这是一个计算机编程问题,我可以回答。以下是Python代码实现:
```python
class Person:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def showinfor(self):
print(f"Name: {self.name}, Age: {self.age}, Sex: {self.sex}")
class Teacher(Person):
def __init__(self, name, age, sex, department):
super().__init__(name, age, sex)
self.department = department
def showinfor(self):
print(f"Name: {self.name}, Age: {self.age}, Sex: {self.sex}, Department: {self.department}")
class Student(Person):
def __init__(self, name, age, sex, marks):
super().__init__(name, age, sex)
self.marks = marks
def showinfor(self):
print(f"Name: {self.name}, Age: {self.age}, Sex: {self.sex}, Marks: {self.marks}")
```
可以使用以下代码测试:
```python
person1 = Person("Alice", 20, "Female")
person1.showinfor()
teacher1 = Teacher("Bob", 30, "Male", "Computer Science")
teacher1.showinfor()
student1 = Student("Charlie", 18, "Male", 90)
student1.showinfor()
```
输出结果:
```
Name: Alice, Age: 20, Sex: Female
Name: Bob, Age: 30, Sex: Male, Department: Computer Science
Name: Charlie, Age: 18, Sex: Male, Marks: 90
```
阅读全文