'''
python常见的继承方法
第一种情况: 在父类的构造函数情况下,额外(扩展父类)加自己的属性,用super
'''
class Employee(object):
def __init__(self, name, id):
self.name = name
self.id = id
def print_info(self):
print(f"员工姓名: {self.name}, 员工id: {self.id}")
class FullTimeEmployee(Employee):
def __init__(self, name, id, monthly_salary):
super().__init__(name, id)
self.monthly_salary = monthly_salary
#重写print_info方法
def print_info(self):
super().print_info()
print(f"================================ {self.monthly_salary}")
class PartTimeEmployee(Employee):
def __init__(self, name, id, daily_salary, work_days):
super().__init__(name, id)
self.daily_salary = daily_salary
self.work_days = work_days
#重写print_info方法
def print_info(self):
super().print_info()
salary = self.daily_salary * self.work_days
print(f"================================ {salary}")
zhoujielun = FullTimeEmployee("zhoujielun", "12332352", 9000)
zhoujielun2 = PartTimeEmployee("zhoujielun2", "12332353", 300, 20)
zhoujielun.print_info()
zhoujielun2.print_info()
python的继承方法super
最新推荐文章于 2024-08-09 09:00:00 发布