定义类CPerson,它有以下属性:姓名、身份证号、性别、年龄、和相应的成员函数(无构造函数)。再利用CPerson派生出类CEmployee,派生类CEmployee增加了两个表示部门和薪水的数据成员,根据需要为派生类增加新的成员函数。
时间: 2024-04-19 08:27:56 浏览: 110
class CPerson:
def __init__(self, name, id_card, gender, age):
self.name = name
self.id_card = id_card
self.gender = gender
self.age = age
class CEmployee(CPerson):
def __init__(self, name, id_card, gender, age, department, salary):
super().__init__(name, id_card, gender, age)
self.department = department
self.salary = salary
def get_department(self):
return self.department
def get_salary(self):
return self.salary
# 可以根据需要添加其他成员函数
# 示例用法
person = CPerson("John", "1234567890", "Male", 30)
employee = CEmployee("Jane", "0987654321", "Female", 35, "HR", 5000)
print(person.name) # Output: John
print(employee.get_department()) # Output: HR
print(employee.get_salary()) # Output: 5000
阅读全文