用Python定义一个people类,包含属性name,sex。为people派生一个之类Eemployee类,为其增加两个属性age和grade。grade分等级A,B,C三个等级。
时间: 2024-05-07 16:16:02 浏览: 143
```python
class People:
def __init__(self, name, sex):
self.name = name
self.sex = sex
class Employee(People):
def __init__(self, name, sex, age, grade):
super().__init__(name, sex)
self.age = age
self.grade = grade
def __str__(self):
return f"Name: {self.name}, Sex: {self.sex}, Age: {self.age}, Grade: {self.grade}"
```
示例:
```python
e = Employee("Tom", "Male", 30, "A")
print(e)
# 输出:Name: Tom, Sex: Male, Age: 30, Grade: A
```
阅读全文