创建人类People,并定义name,age,sex,sn属性,创建People类的子类Student类,并定义stuNo和department属性,创建People另一个子类Teacher,并定义tYear和tDeot属性
时间: 2024-10-11 09:06:00 浏览: 45
在面向对象编程中,我们可以创建一个名为`People`的基本类来表示人类个体,包含通用属性如姓名(name)、年龄(age)和性别(sex)。接着,为了满足特定需求,我们创建两个子类:`Student`和`Teacher`。
```python
class People:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
# 子类 Student 定义
class Student(People):
def __init__(self, name, age, sex, stuNo, department):
super().__init__(name, age, sex)
self.stuNo = stuNo
self.department = department
# 子类 Teacher 定义
class Teacher(People):
def __init__(self, name, age, sex, tYear, tDepartment):
super().__init__(name, age, sex)
self.tYear = tYear
self.tDepartment = tDepartment
```
在上述代码中,`super().__init__(...)`用于调用父类`People`的初始化方法,以便为所有子类共享基本属性。每个子类都有自己的特有属性,例如学生(`stuNo`和`department`)和教师(`tYear`和`tDepartment`)。
阅读全文
相关推荐



















