扩展类继承自基类的一个方法行为,然后如果想要把基类的这个行为重新执行一遍,在自己的这个行为前面加上基类的引用,super后面为啥要加一个括号呢?
直接写super.working()就报错了。。
而且,这里写基类的名字,Employee().working()
也会报错。。只能用super().working(),为啥啊。。
代码如下:
class Employee:
def __init__(self,name,department,title,salary):
self.name = name
self.department = department
self.title = title
self.salary = salary
def __repr__(self):
return f'员工:{self.name}'
def working(self):
print(f'员工{self.name}在工作...')
class Developer(Employee):
def __init__(self,name,department,title,salary,skills):
Employee.__init__(self,name,department,title,salary)
self.skills = skills
def working(self):
super().working()
print('开发人员在工作')
class Accountant(Employee):
def __init__(self,name,department,title,salary,certification):
Employee.__init__(self,name,department,title,salary)
self.certification = certification
if name == '__main__':
d = Developer('tom','技术部','高级工程师','13000',['python','flask'])
print(d.name)
d.working()
慕的地8271018
烙印99
慕桂英4014372
相关分类